PHP CRUD stands for Create, Edit, Update, and Delete. Whenever PHP crud starts, that time will have required database connectivity using PHP.
We will follow the steps to create a PHP CRUD post.
Step 1: Create a Database in table
copy the following code to create a register table in MySQL
CREATE TABLE IF NOT EXISTS `register` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(64) NOT NULL,
`last_name` varchar(64) NOT NULL,
`address` text NOT NULL,
`email` varchar(64) NOT NULL,
`mobile` varchar(12) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;
Step 2: Connect to Database
create connect.php file to connect the tutorial database
#connect.php
<?php
//connect to db
$hostname = "localhost";
$username = "root";
$password = "";
$database = "tutorial"; // tutorial is our db name
$conn = mysqli_connect($hostname, $username, $password, $database);
?>
Step 3: Insert new record in MySQL database
Create an add.php file and add the following code to INSERT a new record in the database
#add.php
<?php
//include db connction file
include('./connect.php');
if (isset($_POST['btnadd'])) {
$first_name = $_POST['txtFname'];
$last_name = $_POST['txtLname'];
$address = $_POST['txtAddress'];
$email = $_POST['txtEmail'];
$mobile = $_POST['txtMobile'];
$insert = "insert into register (first_name,last_name,address,email,mobile) values('$first_name','$last_name','$address','$email','$mobile')";
mysqli_query($conn, $insert);
header('location:index.php');
}
?>
<form method="post" name="frmAdd">
<table align="center">
<tr>
<td colspan="2" align="center">Add Record</td>
</tr>
<tr>
<td>First Name</td>
<td><input type="text" name="txtFname"> </td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="txtLname"> </td>
</tr>
<tr>
<td>Address</td>
<td><textarea name="txtAddress" rows="4" cols="16"></textarea> </td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="txtEmail"> </td>
</tr>
<tr>
<td>Mobile</td>
<td><input type="text" name="txtMobile"> </td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Add" name="btnadd"> </td>
</tr>
</table>
</form>
Step 4: Fetch all the records from the database
Create a new index.php file and add the following code that shows how to retrieve all the records from the database.
#index.php
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<table class="table">
<tr>
<td colspan="7" align="right"><a href="add.php">Add</a></td>
</tr>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Address</th>
<th>Email</th>
<th>Mobile</th>
<th>Action</th>
</tr>
<?php
//include db connction file
include('./connect.php');
$slt = "select * from register";
$rec = mysqli_query($conn, $slt);
while ($row = mysqli_fetch_array($rec)) {
?>
<tr>
<td><?php echo $row['first_name']; ?></td>
<td><?php echo $row['last_name']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['mobile']; ?></td>
<td>
<a href="update.php?edit_id=<?php echo $row['id']; ?>">Edit</a>
<a href="delete.php?delete_id=<?php echo $row['id']; ?>">Delete</a>
</td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</div>
</body>
</html>
Step 5: Php MySQL Update Record
Create a new update.php file in this file to add the following code.
#update.php
<?php
//include db connction file
include('./connect.php');
$edit_id = $_REQUEST['edit_id'];
$slt = "select * from register where id=$edit_id";
$rec = mysqli_query($conn, $slt);
$row = mysqli_fetch_array($rec);
?>
<?php
if (isset($_POST['btnUpdate'])) {
$first_name = $_POST['txtFname'];
$last_name = $_POST['txtLname'];
$address = $_POST['txtAddress'];
$email = $_POST['txtEmail'];
$mobile = $_POST['txtMobile'];
include('connection.php');
$update = "update register set first_name='$first_name',last_name='$last_name',address='$address',email='$email',mobile='$mobile' where id='" . $_REQUEST['edit_id'] . "'";
mysqli_query($conn, $update);
header('location:index.php');
}
?>
<form method="post" name="frmUpdate">
<table align="center">
<tr>
<td colspan="2" align="center">Update Record</td>
</tr>
<tr>
<td>First Name</td>
<td><input type="text" name="txtFname" value="<?php echo $row['first_name']; ?>"> </td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="txtLname" value="<?php echo $row['last_name']; ?>"> </td>
</tr>
<tr>
<td>Address</td>
<td><textarea name="txtAddress" rows="4" cols="16"><?php echo $row['address']; ?></textarea> </td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="txtEmail" value="<?php echo $row['email']; ?>"> </td>
</tr>
<tr>
<td>Mobile</td>
<td><input type="text" name="txtMobile" value="<?php echo $row['mobile']; ?>"> </td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Update" name="btnUpdate"> </td>
</tr>
</table>
</form>
Step 6: Delete Record from MySQL Database
Create a new delete.php file in this file to add the below code is used to delete a particular record from the database.
#update.php
<?php
include('./connect.php');
$delete = "delete from register where id='" . $_REQUEST['delete_id'] . "'";
mysqli_query($conn, $delete);
header("location:index.php);
?>