CRUD Operation in php with live demo and source code

CRUD:-Create, Read, Update and Delete
What New in this turorial:-simple PHP CRUD Operation that performs all tasks like create, select, update and delete on MySql database.

First, we will create a database.
Database Name:-regidb
Database table Name:-regitab

We will insert the following user data into the database table.

Database Table

CREATE TABLE IF NOT EXISTS `regitab` (
  `u_id` int(8) NOT NULL AUTO_INCREMENT,
  `u_fullname` varchar(50) NOT NULL,
  `u_email` varchar(50) NOT NULL,
  `u_password` varchar(50) NOT NULL,
  PRIMARY KEY (`u_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;


config.php

After creating a database, will creating config file to connect a file to MySQL database.
//create database file
<?php 
$server='localhost';
$dbname='regidb';
$username='root';
$userpass='';

$conn=mysqli_connect($server,$username,$userpass,$dbname);

if(!$conn){
echo"could not conneted";
}else{

echo "sucessfuly connected";
}
?>

insert.php

Now, the time has come to add the records to the database. We will insert the value into the database.
//create insert query

<?php
include'config.php';

if(isset($_POST['submit'])){

$name=$_POST['txtname'];
$email=$_POST['txtemail'];
$pass=md5($_POST['txtpass']);
$addr=$_POST['txtadd'];
$mobi=$_POST['txtmobi'];

$query="insert into regitable(name,email,password,address,mobile)values('$name','$email','$pass','$addr','$mobi')"or die('could not connect');

$result=mysqli_query($conn,$query);


}

?>

<form action="insert.php"method="POST">
<label>Name</label>
<input type="text"name="txtname"placeholder="Enter Name"required><br>

<label>Email</label>
<input type="email"name="txtemail"placeholder="Enter Email"required><br>

<label>Password</label>
<input type="password"name="txtpass"placeholder="password"required><br>

<label>Address</label>
<textarea name="txtadd"></textarea><br>

<label>Mobile</lable>
<input type="text"name="txtmobi"value=""size="10"required><br>

<input type="submit"name="submit"value="submit"><br>
</form>

Live Demo

                                                        


select.php

After insert record, we will select data from the database. Each record retrieve based the ID.
//select data
<table cellspacing='5px'cellpadding='5px'>
<th>No.</th>
<th>Name</th>
<th>Email</th>
<th>Address</th>
<th>Mobile</th>
<th>action</th>
<?php 
include'config.php';
$query="select *from regitable";
$result=mysqli_query($conn,$query);
$cnt='A';
while($row=mysqli_fetch_array($result)){
?>
<tr>
<td><?php echo "($cnt)";?></td>
<td><?php echo $name=$row['name'];?></td>
<td><?php echo $email=$row['email'];?></td>
<td><?php echo $addr=$row['address'];?></td>
<td><?php echo $mobi=$row['mobile'];?></td>
<td><a href="edit.php?id=<?php echo $row['id'];?>">Edit</a> |
<a href="delete.php?id=<?php echo $row['id'];?>">delete</a></td>
</tr>
<?php $cnt++;
} ?>
</table>

Live Demo

                                                    

delete.php

To delete a record from the database, add delete links. We will delete the record based on ID.
//delete data

<?php 
include'config.php';

$id=$_GET['id'];

$query="delete from regitable where id=$id";

$result=mysqli_query($conn,$query);

header('location:select.php');

?>

Live Demo

                                                    

update.php


If you add the wrong data to the database at that time, then we need to update the data. In this script, we will update the data based on ID
<html>
<head>
<head>
<body>

<?php

include'config.php';
if(isset($_POST['update'])){

$id=$_GET['id'];
$name=$_POST['txtname'];
$email=$_POST['txtemail'];
$address=$_POST['txtadd'];
$mobile=$_POST['txtmobi'];

$query="update regitable set name='$name',email='$email',address='$address',mobile='$mobile'";
$result=mysqli_query($conn,$query);
header('location:select.php');
}
?>

<!--select data -->
<?php 
$id=$_GET['id'];

$query=mysqli_query($conn,"select *from regitable where id=$id");
while($row=mysqli_fetch_array($query)){

 $name=$row['name'];
 $email=$row['email'];
 $addr=$row['address'];
 $mobi=$row['mobile'];

}

?>

<form action="edit.php"method="POST">
Name
<input type="text"name="txtname"value="<?php echo $name;?>"required><br>
Email
<input type="email"name="txtemail"value="<?php echo $email;?>"required><br>
Address
<textarea resize="none" name="txtadd" cols="20"rows="5"required><?php echo $addr;?></textarea><br><br>

Mobile
<input type="text"name="txtmobi"value=<?php echo $mobi;?>>

<input type="hidden"name="id"value=<?php echo $_GET['id'];?>>

<input type="submit"name="update"value="update">


Live Demo

                                                                    




        
                                                                    
Download Source code

Comments