CRUD Operation in PHP With Live Example!

What is CRUD?

CRUD operations are basic information manipulation for databases. We’ve already learned a way to perform produce (i.e. insert), read (i.e. select), update, and delete operations in previous chapters. during this tutorial, we’ll produce a straightforward PHP application to perform these operations on a MySQL information table in one place.

What’s CRUD Full Form?

CRUD is a word form for Create, Read, Update, and Delete.

CRUD operation in PHP MySQL

Well, let’s begin by making the table that we’ll use all told of our examples.

  1. Create new Database
  2. Create new table
  3. Create index.php file
  4. Create server.php file
  5. Create style.css file

1.Create new Database

create new database ( crud ). crud is database name.

2.Create new table

create new table ( info ). info is table name.

3.Create index.php file

create an index.php file on your computer and create a form now add PHP in your index.php file and include your server.php file also.


// fetch the record to be updated
	if (isset($_GET['edit'])) {
		$id = $_GET['edit'];
		$edit_state = true;
		$rec = mysqli_query($db, "SELECT * FROM info WHERE  id='$id'" );
		$record =mysqli_fetch_array($rec);
		$name = $record['name'];
		$address = $record['address'];
		$id = $record['id'];


<!DOCTYPE html>
	<title>crud one page</title>
	<link rel="stylesheet" type="text/css" href="style.css">
	<style type="text/css">
	margin:30px auto;
	padding: 10px;
	border-radius: 5px;
	color: yellow;
	width: 50%;
	text-align: center;

<?php if (isset($_SESSION['msg'])): ?>
<div class="msg">
		echo $_SESSION['msg'];
<?php endif ?>

		<th colspan="2">action</th>
<?php while ($row = mysqli_fetch_array($results)) {  ?>
		<td><?php echo $row['name']; ?></td>
		<td><?php echo $row['address']; ?></td>
			<a href="index.php?edit=<?php echo $row['id']; ?>">edit</a>
			<a href="server.php?del=<?php echo $row['id']; ?>">delete</a>


<form method="post" action="">
	<input type="hidden" name="id" value="<?php echo $id; ?>">
	<div class="input-group">
		<input type="text" name="name" value="<?php echo $name; ?>" >
	<div class="input-group">
		<input type="text" name="address" value="<?php echo $address; ?>">
	<div class="input-group">
		<?php if ($edit_state == false) : ?>
		<button type="submit" name="save" class="btn">save</button>
		<?php else: ?>
		<button type="submit" name="update" class="btn">update</button>
		<?php endif ?>



4.Create server.php file

now create server.php file in computer and PHP code written first insert query, second view query, third update query, and last four delete query insert in server.php


// initialize variables
	$name = "";
	$address = "";
	$id = 0;
	$edit_state = false;

// connect to database

// if save btn is click
$db = mysqli_connect('localhost','root','','crud');

if (isset($_POST['save'])) {
	$name = $_POST['name'];
	$address = $_POST['address'];

	$query = "INSERT INTO info (name,address) VALUES ('$name','$address')";
	$_SESSION['msg']  = "address delete";
			header('location:index.php');//redirect to index page after inserting 

//update record
		if (isset($_POST['update'])) {
			$name = mysqli_real_escape_string($db, $_POST['name']);
			$address = mysqli_real_escape_string($db, $_POST['address']);
			$id = mysqli_real_escape_string($db, $_POST['id']);

			$data= mysqli_query($db, "UPDATE info SET name='$name', address='$address' WHERE id='$id' ");
			if ($data) {
					echo "<script>alert('update')</script>";

// delete record

	if (isset($_GET['del'])) {
		$id = $_GET['del'];
		mysqli_query($db, "DELETE FROM info WHERE id='$id'");
				$_SESSION['msg']  = "address delete";


//retrieve records
	$results = mysqli_query($db, "SELECT * FROM info");


5.Create style.css file

You add steel.css to make your crud table look good.

	font-size: 20px;
	width: 50%;
	margin:30px auto;
	border-collapse: collapse;
	text-align: left;
	border-bottom: 1px solid black;
tr, td{
	border: none;
	height: 30px;
	padding: 3px;
tr:hover {
	width: 50%;
	margin: 50px auto;
	text-align: left;
	padding: 20px;
	border: 1px solid black;
	border-radius: 5px;
	margin: 10px 0px 10px 0px;
.input-group label{
	display: block;
	text-align: left;
	margin: 3px;
.input-group input{
	height: 30px;
	width: 93%;
	padding: 5px 10px;
	font-size: 16px;
	border-radius: 5px;
	border: 1px solid gray;
padding: 10px;
font-size: 15px;
color: white;
border-radius: 5px; 


How to Create CRUD ??

CRUD means is Create, Read, Update, and Delete.

CRUD ( Insert, Show, Update and delete ) in PHP zip.file here Download link.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *