DELETE RECORDS IN PHP/MYSQL


Delete Records in mysql


Delete records from database is a easy task the main logic behind deleting a record from database table is delete command of my sql as follows

Delete from tableName where id = something



The data you want to delete will be pass in id and the record will be deleted from database make sure if you delete a record from database you can't recover that record .


You need following things for deleting record


First you have to connect your script with database than make sure you are passing the id for that record you wish to delete .

Php code to delete the record from database.


<?php
require_once('connection.php');
$id = $_GET['id'];
$query = "DELETE FROM `crud` WHERE id=$id"; //query for delete record
$result = mysqli_query($connection, $query);
if($result){
header('location: display.php');
}else{
echo "Can't delete at this moment please try again";
header( "refresh:5;url=display.php" );
}
?>

If have any query related to this code kindly comment in comment-box.

Read more »

CRUD IN PHP

What is crud ?



Crud stands for create , read , update and delete operations in database if you are new to php and want to experience crud operations in php than you are right place if you are a professional developer you have to use these operations in every span of your proagraming language if you are beginner don't worry at all i will explain here all these php things step by step.




Steps for Crud operations in php



  • Create a database as you wish but for demonstration purpose i am using my database name as "php_operations".
  • Enter sql file in your sql tab after creating database and your database with table is ready.



Now we need following files for crud operation


We need a system that can enter records in table so here i am using index.php to insert record in mysql.The logic behind this operation is insert query of mysql.

After insert operation we need another file that can display all the records on your screen for this thing i am using "display.php" file here.This file will read the records from mysql table and display them on the screen.The main logic behind this file is select command of mysql select command will select all ths data in mysql table and php will display all these records on screen we will also provide two more links in this table one for update record i.c we will link update link to update.php that will take id of particular record from display file to update.php to update the record in database.
The second link will be delete link a id will also pass from display.php to delete.php so that it can conclude which record it have to delete.

so i am using update.php for record updation and delete.php for deleting record.For updation the main logic behind it is update query while in delete.php is sql delete query.Kindly notice the delete.php file will delete the record and again pass control to display.php for more details watch the video above  find code for these files as given below :















If you have any quer regarding this tutorial  kindly leave a comment in commentbox


Read more »

Update Records in mysql datebase

Update Records in Mysql :


It is easy to update records in mysql database using html forms.After creating insert record in mysql we can easily update records but make sure while using update you have to pass id of a particular record from previous script like the url will take particular id to that script which will take part in updating the particular record.In this case display.php is script that will display record and when user will click on update than display.php file will pass the id of particular record that user clicked using the query string or url and than update.php file will receive that id and select the particular record and display all the data into form and when user will click on update button in upadate.php than it will take action and update the record and if record updation is successfull than again it will redirect the user to display.php using header function if failed to update record it will display a failed message to user. If you are unable to understand make sure you go throung "Insert records" and "display records"




Logic Behind Updating Record :


The main logic behind updation record is update command of sql as follows

Update  tablename set tablefield = value where id =  --


Sql for database

i am using 'php_operations' as database name and crud as table name so create the database with name of php_operations and put the following sql in sql tab of mysql as instructed in video.



CREATE TABLE `crud` (
`id` int(11) NOT NULL,
`firstName` varchar(255) NOT NULL,
`lastName` varchar(255) NOT NULL,
`gender` varchar(255) NOT NULL,
`age` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `crud` (`id`, `firstName`, `lastName`, `gender`, `age`, `email`) VALUES
(1, 'jeff', 'morgan', 'male', '42', 'jeff@gmail.com'),
(2, 'stenly', 'uba', 'female', '21', 'stenly@gmail.com'),
(3, 'pablo', 'morgan', 'male', '98', 'pablo@gmail.com'),
(4, 'susan', 'arya', 'female', '22', 'susan@yahoo.com');
ALTER TABLE `crud` ADD PRIMARY KEY (`id`);

ALTER TABLE `crud`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;


The file Display.php


For updating record first we need to display the records with a update button so here is the code that will send data to update.php file for communication



<!DOCTYPE html>
<html>
<head>
 <title>Simple CRUD Application - READ Operation</title>
 <!-- Latest compiled and minified CSS -->
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
</head>
<body>
 <div class="container">
 <div class="row">
 <h1><center>Fetch Operation using php - crud</center></h1>
 <table class="table ">
 <thead> <tr>
 <th>#</th>
 <th>Full Name</th>
 <th>E-Mail</th>
 <th>Age</th>
 <th>Gender</th>
 <th>Update Record</th>
 <th>Delete Record</th>
 </tr>
 </thead>
 <tbody>

<?php
 require_once('connection.php');
 $query = "SELECT * FROM `crud`";
 $result = mysqli_query($connection, $query);
 while($row = mysqli_fetch_assoc($result)){
?>
 <tr>
 <th scope="row"><?php echo $row['id']; ?></th>
 <td><?php echo $row['firstName'] . " " . $row['lastName']; ?></td>
 <td><?php echo $row['email']; ?></td>
 <td><?php echo $row['gender']; ?></td>
 <td><?php echo $row['age']; ?></td>
 <td>
 <a href="update.php?id=<?php echo $row['id']; ?>"><span class="glyphicon glyphicon-edit" aria-hidden="true"></span></a>
 </td>
 <td>
 <a href="delete.php?id=<?php echo $row['id']; ?>"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></a>
 </td>
 </tr>
<?php } ?>
 </tbody>
 </table>
 </div>
 </div>
</body>
</html>



Update.php for updatetion of reocrds

This file will update the records in database when user will click on the Update button by changing records in form and this will redirect user back to display.php file for showing the updated record.


<!DOCTYPE html>
<html>
<head>
 <title>Update Operations in php</title>
 <!-- Latest compiled and minified bootstrap CSS -->
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
</head>
<body>
 <div class="container">
<?php
 error_reporting(0);
 require_once('connection.php');
 $id = $_GET['id'];
 $queryl = "SELECT * FROM `crud` WHERE id=$id";
 $result1 = mysqli_query($connection, $queryl);
 $row = mysqli_fetch_assoc($result1);
?>
 <div class="row">
  <form method="post" class="form-horizontal col-md-6 col-md-offset-3">
  <center><h2>Execute crud operations</h2></center>
<br/><hr/>
  <div class="form-group">
 <label for="item1" class="col-sm-3 control-label">First Name</label>
 <div class="col-sm-9">
 <input type="text" name="f_name" class="form-control" id="item1" value="<?php echo $row['firstName']; ?>" placeholder="First Name" />
 </div>
 </div>
 <div class="form-group">
 <label for="item2" class="col-sm-3 control-label">Last Name</label>
 <div class="col-sm-9">
 <input type="text" name="l_name" class="form-control" id="item2" value="<?php echo $row['lastName']; ?>" placeholder="Enter Your Last Name" />
 </div>
 </div>
 <div class="form-group">
 <label for="item3" class="col-sm-3 control-label">E-Mail</label>
 <div class="col-sm-9">
 <input type="email" name="email" class="form-control" id="item3" value="<?php echo $row['email']; ?>" placeholder="Your Email" />
 </div>
 </div>
 <div class="form-group" class="radio">
 <label for="input1" class="col-sm-3 control-label">Gender</label>
 <div class="col-sm-9">
 <label>
 <input type="radio" name="gender" id="radios1" value="male" <?php   if($row['gender'] == 'male'){ echo "checked";} ?>> Male </label>
 <label>
 <input type="radio" name="gender" id="optionsRadios1" value="female" <?php if($row['gender'] == 'female'){ echo "checked";} ?>>feMale
 </label>
 </div>
 </div>
 <div class="form-group">
 <label for="item4" class="col-sm-3 control-label">Age</label>
 <div class="col-sm-9">
 <input type="text" name="age" class="form-control" id="item4" value="<?php echo $row['age']; ?>" placeholder="Your Age" />
 </div>
 </div>
 <input type="submit" class="btn btn-danger col-md-2 col-md-offset-10" value="Update" />
 </form>
 </div>
 </div>
</body>
</html>
<?php
 if(isset($_POST) & !empty($_POST)){
 $f_name = $_POST['f_name'];
 $l_name = $_POST['l_name'];
 $email = $_POST['email'];
 $gender = $_POST['gender'];
 $age = $_POST['age'];

 $query2 = "UPDATE `crud` SET firstName='$f_name', lastName='$l_name', gender='$gender', age=$age, email='$email' WHERE id=$id";
 $result2 = mysqli_query($connection, $query2);
 if($result2){
 header('location: display.php');
 }else{
 $faildMsg = "Data can't be updated try again.";
 }
}
?>
 <?php if(isset($faildMsg)){ ?><div class="alert alert-danger" role="alert"><center> <?php echo $faildMsg; ?></center></div><?php?>





Feel free to comment below if have any issue regarding code.
Read more »

Create facebook account without email and mobile number

Create Account on facebook without email and mobile 

Facebook is secure among social web site platforms but it have some loop holes. Yes you can create or sign up on facebook without having a email account or without mobile number.

How it is possible on facebook



Facebook is social platform who usually authenticate his users by sending one time password on mobiles or by sending a six digit verification code on emails.When user fill this one time password than facebook confirm that user as a trustworthy and previous registered user.

But you can betray facebook because their are many mail servers available now a day that allow people to generate a email id for temporary basis or for some minutes in this small time span a user can receive one time password or facebook secure code on these email id's and confirm himself as a trustworthy user. If you don't believe or think signup without email or mobile is a rumor you can watch the video demonstrated below or you can follow these steps for creating a account.





Web sites that allow you to generate email id


  https://temp-mail.org/en/

  https://www.tempmailaddress.com

  https://getnada.com

  www.20minutemail.com/


Mind the follwing things to create account on facebook 

Make sure your laptop location is off.

Change your ip address.

If you are using iphone or ipad change the ip address.

While using temproray mail make sure you have opened this mail in private window or in guest mode .

You may use another browser for reviving your code if you open same mail server in same browser without using guest window than facebook will ask you mobile number but if you are using it as described in video than sure you can sign up on facebook without having email or mobile number.

Precaustions while using facebook account 


It is good idea to sign up facebook without these stuff but keep in mind following factors while using your facebook account

If you are sending request to unknown people or too much request or spamming using this account and facebook doubt this fact than you can't recover your account or you can't clerify to facebook that you are owner of account because the mail you have used while creating the website will be expired now and facebook will send secure code to that email only .
Read more »

Fetch Data from database

Fetch data from database :


Reading data from database is easy process .To fetch or read data from database using php you need to require some knowledge of php language and its functions and how you can enter records in mysql or insert operation or just enter the following sql file in sql tab of database as instructed in video i am using 'php operations' as database or you may create it accordingly.



Sql file to read data from database


CREATE TABLE `crud` (
`id` int(11) NOT NULL,
`firstName`
varchar(255) NOT NULL,
`lastName` varchar(255) NOT NULL,
`gender` varchar(255) NOT NULL,
`age` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `crud` (`id`, `firstName`, `lastName`, `gender`, `age`, `email`) VALUES
(1, 'jeff', 'morgan', 'male', '42', 'jeff@gmail.com'),
(2, 'stenly', 'uba', 'female', '21', 'stenly@gmail.com'),
(3, 'pablo', 'morgan', 'male', '98', 'pablo@gmail.com'),
(4, 'susan', 'arya', 'female', '22', 'susan@yahoo.com');
ALTER TABLE `crud` ADD PRIMARY KEY (`id`);
ALTER TABLE `crud`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;


After creating a table into database the real logic behind the scene comes into play.First of all we require a connection to connect to mysql database so here is connection.php file which will use mysql_select_db for selecting database and mysql_connect function for connecting php to database and die function if there is no connection or no such database will exist than it will terminate the connection.


<?php
 $connection = mysqli_connect('localhost', 'root', '');
  if (!$connection){
  die("Database Connection Failed" . mysqli_error($connection));
  }
  $selectDb = mysqli_select_db($connection, 'php_operations');
  if (!$selectDb){
  die("Unable to select database" . mysqli_error($connection));
  }
?>



When connection.php file is ready , we are good to go and can fetch data from database

Fetch or Read data from database :


To read record from database we need following logic , first we have to know that while reading data from tables what is going behind actually we are selecting rows from tables and than displaying the result with the help of php. So the sql operation will be select a record.Than there are some php functions that will play their roles. mysql_query will execute sql commend and return result to result variable in the script.This will select all our table as a resource than we will use mysql_fetch_assoc function that will fetch the result as a associative array in key and value form and return result as row.Than we can display this array result with the help of while loop in the forms of tables or we can customize this result as we wish to display it but in most cases we require in tables.Here is "display.php" file that will display the whole record to fetch records from database.



<!DOCTYPE html>
<html>
<head>
<title>Read data from mysql - CRUD</title>
<!-- Latest compiled bootstrap for layout and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
</head>
 <body>
  <div class="
container">
  <div class="
row">
  <h1><center>Fetch Operation using php - crud</center></h1>


  <br/><hr/>
   <table class="
table ">
    <thead>
     <tr>
      <th>#</th>
      <th>Full Name</th>
      <th>E-Mail</th>
      <th>Age</th>
      <th>Gender</th>
      <th>Update Record</th>
      <th>Delete Record</th>
    </tr>
    </thead>
     <tbody>
  <?php
    require_once('connection.php');
    $query = "SELECT * FROM `crud`";
    $result = mysqli_query($connection, $query);
    while($row = mysqli_fetch_assoc($result)){
  ?>
   <tr>
   <th scope="row"><?php echo $row['id']; ?></th>
     <td><?php echo $row['firstName'] . " " . $row['lastName']; ?>       </td>
     <td><?php echo $row['email']; ?></td>
     <td><?php echo $row['gender']; ?></td>
     <td><?php echo $row['age']; ?></td>
     <td>
       <a href="update.php?id=<?php echo $row['id']; ?>"><span class="glyphicon glyphicon-edit" aria-hidden="true"></span></a>
     </td>
    <td>
      <a href="delete.php?id=<?php echo $row['id']; ?>"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></a>
    </td>
   </tr>
  <?php } ?>
 </tbody>
 </table>
 </div>
</div>
</body>
</html>



If have any question related to fetch record from database kindly comment in comment-box.

Read more »

Insert Data into mysql


Insert data into mysql 


If you are a beginner or a php proagrammer you must learn how to insert data into database using php.Here is the story for insertion data into database. First of all you require to login into your server or in your xampp and then create a database i named it as ''php operations" here and insert the database by entering the following sql commands in sql tab of mysql.For more information you may watch the following video or just copy and paste the code below .





CREATE TABLE `crud` (
`id`
int(11) NOT NULL,
`firstName`
varchar(255) NOT NULL,
`lastName`
varchar(255) NOT NULL,
`gender`
varchar(255) NOT NULL,
`age`
varchar(255) NOT NULL,
`email`
varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `crud` ADD PRIMARY KEY (`id`);
ALTER TABLE `crud`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;



Now our database is ready .I uses bootstap there for layout if you don't have knowledge of bootstrap don't worry just ignore the classes in html and concentrate on php logic.


Connect to database for insertion


After creating database our next step is to make a connection file using php so that we can connect to our database for this thing we need our localhost or server name and the username for host and password we will use mysql_select function for database selection and mysql_connect function for make sure that we have successfully connected to database. Here i am using connection.php  file for providing connection to database in index.php file.



<?php
$connection = mysqli_connect('localhost', 'root', '');
 if (!$connection){
  die("Database Connection Failed" . mysqli_error($connection));
 }
  $selectDb = mysqli_select_db($connection, 'php_operations');
 if (!$selectDb){
  die("Unable to select database" . mysqli_error($connection));
 }
?>

We are ready to connect now we will create index.php  file that will contain lot of functions as given below.

Insert data in database create index.php file


First we need connection in index file so we need require_once function than we will check if there is something coming from form i.e check if post is set and noting is empty than we will collect all form data into variables and after collection we will check for that all field have data if all field don't have data than it will display "kindly fill all fields" if all things are ready to go than we will execute a query for database insertion using mysql_query function it will return resource in result variable and display a success messege to user.


<!DOCTYPE html>
<html>
 <head>
  <title>
Data Insertion in mysql database using php</title>
   <!-- Latest bootstrap compiled and minified CSS -->
   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
>
 </head>
<body>
 <div class="container">
  <div class="row">
   <form method="post" class="form-horizontal col-md-6 col-md-offset-3">
   <center><h2>Execute Insert operation</h2></center>
<br/><hr/>
    <div class="form-group">
     <label for="item1" class="col-sm-3 control-label">First Name</label>
    <div class="col-sm-9">
     <input type="text" name="f_name" class="form-control" id="item1" placeholder="Enter Your First Name" />
    </div>
  </div>

    <div class="form-group">
     <label for="item2" class="col-sm-3 control-label">Last Name</label>
    <div class="col-sm-9">
     <input type="text" name="l_name" class="form-control" id="item2" placeholder="Enter Your Last Name" />
    </div>
   </div>
    <div class="form-group">
     <label for="item3" class="col-sm-3 control-label">E-Mail</label>
    <div class="col-sm-9">
      <input type="email" name="email" class="form-control" id="item3" placeholder="Your Email" />
    </div>
    </div>
    <div class="form-group" class="radio">
     <label for="input1" class="col-sm-3 control-label">Gender</label>
    <div class="col-sm-9">
     <label>
     <input type="radio" name="gender" id="radio1" value="male" checked> Male
</label>
    <label>
     <input type="radio" name="gender" id="radios2" value="female"> Female
    </label>
   </div>
   </div>
    <div class="form-group">
     <label for="item4" class="col-sm-3 control-label">Age</label>
    <div class="col-sm-9">
     <input type="text" name="age" class="form-control" id="item4" placeholder="Your Age" />
     </div>
    </div>
     <input type="submit" class="btn btn-danger col-md-2 col-md-offset-10" value="submit" />
   </form>
   </div>
 </div>
</body>
</html>

<?php
 

 require_once ('connection.php');

  if(isset($_POST) && !empty($_POST)){
  $f_name = $_POST['f_name'];
  $l_name = $_POST['l_name'];
  $email  = $_POST['email'];
  $gender = $_POST['gender'];
  $age    = $_POST['age'];
   if(!empty($f_name) && !empty($l_name) && !empty($email )&& !empty($gender) && !empty($age)){
   $query = "INSERT INTO `crud` (firstName, lastName, email, gender, age) VALUES ('$f_name', '$l_name', '$email', '$gender', '$age')";
   $result = mysqli_query($connection, $query) or die(mysqli_error($connection));
   if($result){
    $success = "Your data has been Inserted into databse";
   }else{
   $failed = "Something went wrong please try another time";
   }
  }
  else{
    echo "<hr><div class='alert alert-danger' ><center>Kindly fill all the fields</center></div>";
 }
}
?>
<?php if(isset($success)){ ?><hr><div class="alert alert-success" ><center> <?php echo $success; ?></center> </div><?php } ?>
<?php if(isset($failed)){ ?><hr><center><div class="alert alert-danger" > <?php echo $failed; ?> </div></center><?php } ?>


If have any question kindly leave a comment in comment box.
Read more »

Validate Email In Php

Validate Email Using Php


Sometimes you need to validate a email when you use php as proagraming language there are many other ways to validate a email like jquery validation is popular but Php Email validation is more secure than front end validation . To validate a email using php we require php filter_var() function .






filter_var()


filter_var() function filters the result by consuming filter provided in it

This function accept three parameters and than filter out the result as follows

filter_var( variableName , filterName , options )  the last parmeter is optional and it check each filter with possible options



FILTER_VALIDATE_EMAIL

We will use FILTER_VALIDATE_EMAIL filter so that we can filter or validate a given email

we can also check for illigle characters in email by using FILTER_SANITIZE_EMAIL filter

let's see how we will filter email. just copy the following code in your xampp htdocs folder and try to run the script

<?php

 if ( isset ( $_GET ) && !empty ( $_GET ) ){

    $email  =  $_GET['email'];
 
 
    if ( filter_var ( $email , FILTER_VALIDATE_EMAIL) ){

 
        echo "<h2>$email is a valid email.</h2>";   



      }else{


        echo "<h2>$email is not a valid email.</h2>";

   }
 }
   
?>
<form   method = "get" action="">

   <p>Enter your email address : <input type="text" name="email" ></p>
  

   <input type="Submit" value="Submit">

</form>



If have any question kindly leave a comment .


Read more »