CRUD Operation By PHP and Database

Hey, Coders we want to cover CRUD operation by using PHP and a database. In this tutorial, we learn basic and advanced concepts of PHP and databases.

We perform CRUD Operations

C: CREATE // insert data to the database by using form elements

R: Read // Fetching data from the database by using table tags

U: Update // Updating existing data to others by using form elements

D: Delete // Removing data from the database completely.

Let’s cover all things one by one starting from database creation to deleting data from the database.

How To Create Database?

To create a database we must start our local server of Apache and MySQL.

After starting local server type localhost://phpmyadamin

You can get the following interfaces

The above images show how to create a database by using PHPMyAdmin

Create database name phptutdb

After creating the database have to create tables and name them employees as we suggested it on the previous part of this tutorial.

Create con.php

In con. db we create the database connection and use it everywhere when we need it and include it.

<?php
$server="localhost";
$user="root";
$password="";
$db="phptutdb";

$dbconnection=mysqli_connect($server,$user, $password,$db);
if($dbconnection){
	
}

else{
	echo "Connection Not Established";
}
?>

The code is used to connect PHP to the database.

Create add.php

In these parts, we used to create a form that used to insert data into to database by using post methods.

The code is shown below

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title></title>
	<?php
		include 'con.php'
	if(isset($_POST['save']))
{
$server="localhost";
$user="root";
$password="";
$db="phptutdb";


	$fullname=$_POST['full_name'];
	$gender=$_POST['gender'];
	$department=$_POST['department'];
	$salary=$_POST['salary'];
	$address=$_POST['address'];
	$phone=$_POST['phone'];

	$insert="insert into employee(full_name,gender,department,salary,phone,address)
			 values('$fullname','$gender','$department','$salary','$address','$phone')";
	$query=mysqli_query($dbconnectin,$insert);
	if($query){
		echo "Recorded Added Successfully";
	}

}

}
?>
</head>
<body>
<form action="#" method="POST">
	<label>Full Name</label><br>
	<input type="text" name="full_name" required><br>

	<label>Gender </label><br>
	<input type="text" name="gender" required><br>

	<label>Salary</label><br>
	<input type="number" name="salary"><br>

	<label>Department</label><br>
	<input type="text" name="department"><br>

	<label>Phone</label><br>
	<input type="text" name="phone"><br>

	<label>Address</label><br>
	<input type="text" name="address"><br>

	<br>
	<input type="submit" value="Save" name="save">
	
</form>
</body>
</html>

the above code is used to insert data into the database by using the form.

After inserting data into the database fetch data from the database and display it by using tables.

To fetch data from the database follow the following steps.

Step 1: create file list.php

List.php file is used to display data from the database in the form of a table the code is as below

You can copy the code and paste it into your code.

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>List of Employeess</title>
</head>
<body>
	<h2>List of Employees</h2>
	<a href="add.php"> Add</a>
	<table border="1">
		<thead>
			<tr>
				<th>#</th>
				<th>Full Name</th>
				<th>Department</th>
				<th>Salary</th>
				<th>Action</th>
			</tr>
		</thead>
		<tbody>
			<?php 

			include "dbcon.php";
			 $query="select * from employee";
			 $query_run=mysqli_query($con,$query);
			 if(mysqli_num_rows($query_run) > 0){
			 	while($row= mysqli_fetch_assoc($query_run)){
			 		?>
			 		<tr>
			 			<td><?php echo $row['id'];?></td>
			 			<td><?php echo $row['full_name'];?></td>
			 			<td><?php echo $row['department'];?></td>
			 			<td><?php echo $row['salary'];?></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 
			 	}
			 }

			?>
			
		</tbody>
		
	</table>

</body>
</html>

the above code is used to display data from the database.

the above image shows the output list of employees from the database.

Edit

Create an edit.php file that is used to display data from a database that is fetched by employee ID and put on edit fields. Edit form is similar to the add.php file but the difference is edit has value and add has no value on form.

edit.php

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Update Form</title>

	<?php 
	include 'dbcon.php';

	$id=$_REQUEST['id'];
	$query="select * from employee where id= '$id'";
	$query_run= mysqli_query($con,$query);
	$row=mysqli_fetch_array($query_run);
   ?>
</head>
<body>
	<h2>Employee Update Form</h2>
	<form action="#" method="post">
		<input type="hidden" name="id" value="<?php echo $row['id']?>">
		<label>Full Name</label> <br>
		<input type="text" value="<?php echo $row['full_name']?>" name="full_name"><br>
		<label>Gender</label> <br>
		<input type="text" value="<?php echo $row['gender']?>"  name="gender"><br>
		<label>Department</label> <br>
		<input type="text" value="<?php echo $row['department']?>" name="department"><br>
		<label>Salary</label> <br>
		<input type="text" value="<?php echo $row['salary']?>" name="salary"><br>
		
		<label>Address</label> <br>
		<input type="text" value="<?php echo $row['address']?>"  name="address"><br>
<br>
		<button type="submit" name="update">Update </button>
	</form>

	<?php

if(isset($_POST['update'])){
	$id=$_POST['id'];
	echo $id;
	$full_name=$_POST['full_name'];
	$gender=$_POST['gender'];
	$department=$_POST['department'];
	$salary=$_POST['salary'];
	$address=$_POST['address'];

	$update_query="update employee set full_name='$full_name',department='$department',gender='$gender',salary='$salary',address='$address' where id='$id'";
	$update_query_run=mysqli_query($con,$update_query);
	if($update_query_run){
		echo "Updated Success!!";
	}

}
	 ?>
</body>
</html>

The above code shows edit value based on the ID you select or click from the list of employees.

Delete

Delete is the process of removing data from the database. To delete data from the database follow the following

Create a delete.php file and paste the following code on it

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Delete Data</title>
</head>
<body>

<?php

include 'dbcon.php';

$id=$_REQUEST['id'];

$query="delete from employee where id= '$id'";
$query_run = mysqli_query($con,$query);
if($query_run){
	echo "Data was Successfully Deleted";
}
else{
	echo "Failed to Delete data pleasw try later!!";
}

 ?>

</body>
</html>

The above code is used to delete data from the database by clicking the delete button.

To Delete data must exist in the database.

Download Source code

You can also download source code from the link below and you can use it.

Download Link here https://bbn11.com/wp-content/uploads/2024/04/registration.zip

Steps to run the project

step 1: Install local server xampp or wamp

Step 2: Start the Xampp server create a database and name it phpcrud_db

step 3: Extract the downloaded code copy it to the htdocs folder and call the project name

step 4: Open your browser localhost/register/list.php

Summary

In general, you can learn more about how to insert data by using the interface, how to fetch data from the database, how to edit data to the database, and how to delete data from the database by using PHP and MySQL database simply.

We have learned what is CRUD.

CRUD means active data query in PHP, CREATE, READ, EDIT, DELETE

Remember: Telegram Channel Borufan Tube(https://t.me/tobcoders)

Youtube Channels Borufan Tube(https://www.youtube.com/@borufantube)

Author: bbnadmin

1 thought on “CRUD Operation By PHP and Database

Leave a Reply

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