Learn how to perform basic CRUD (Create, Read, Update, Delete) operations in PHP using MySQL with practical examples. This guide will walk you through building a simple CRUD application using procedural PHP and MySQLi functions. Whether you’re a beginner or brushing up on your PHP skills, this tutorial is a great place to start mastering PHP-MySQL integration.
index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="students_add.php" method="post">
<table border="1" width="60%" align="center">
<tr>
<td align="center" colspan="2">
<h1>ADD STUDENTS</h1>
</td>
</tr>
<tr>
<td>Students Name</td>
<td><input type="text" name="studname" id=""></td>
</tr>
<tr>
<td>Students course</td>
<td>
<select name="course" id="">
<option value="bca">BCA</option>
<option value="bscit">BSCIT</option>
<option value="bcom">BCOM</option>
<option value="mcom">MCOM</option>
</select>
</td>
</tr>
<tr>
<td>Students City</td>
<td><input type="text" name="city" id=""></td>
</tr>
<tr>
<td>Students Age</td>
<td><input type="text" name="age" id=""></td>
</tr>
<tr>
<td>Students Mobile</td>
<td><input type="text" name="mobile" id=""></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="ADD">
</td>
</tr>
</table>
</form>
<hr>
<table border="1" width="100%">
<tr>
<td align="center" colspan="7">
<h1>
STUDENT'S DATA
</h1>
</td>
</tr>
<tr>
<th>ID</th>
<th>NAME</th>
<th>COURSE</th>
<th>CITY</th>
<th>AGE</th>
<th>MOBILE</th>
<th>ACTION</th>
</tr>
<?php
$conn=mysqli_connect("localhost","root","","hvc");
$qry="select * from students order by id desc";
$result = mysqli_query($conn,$qry);
while($row=mysqli_fetch_array($result)){
?>
<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["studname"]; ?></td>
<td><?php echo $row["course"]; ?></td>
<td><?php echo $row["city"]; ?></td>
<td><?php echo $row["age"]; ?></td>
<td><?php echo $row["mobile"]; ?></td>
<td>
<a href="students_edit.php?id=<?php echo $row["id"]; ?>">Edit</a>
<a href="students_delete.php?id=<?php echo $row["id"]; ?>">Del</a>
</td>
</tr>
<?php
}
?>
</table>
</body>
</html>
students_add.php
<?php
extract($_POST);
$conn=mysqli_connect("localhost","root","","hvc");
if(!$conn){
echo "connection failed";
}else{
echo "connection success";
$qry="insert into students (studname,course,city,age,mobile)values('$studname','$course','$city','$age','$mobile')";
mysqli_query($conn,$qry);
header("location:index.php");
}
?>
students_delete.php
<?php
$id = $_REQUEST["id"];
$conn = mysqli_connect("localhost", "root", "", "hvc");
$qry = "delete from students where id='$id'";
mysqli_query($conn, $qry);
header("location:index.php");
?>
students_edit.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
$id=$_REQUEST["id"];
$conn = mysqli_connect("localhost", "root", "", "hvc");
$qry = "select * from students where id='$id'";
$result = mysqli_query($conn, $qry);
$row = mysqli_fetch_array($result);
?>
<form action="students_update.php" method="post">
<table border="1" width="60%" align="center">
<tr>
<td align="center" colspan="2">
<h1>UPDATE STUDENTS</h1>
</td>
</tr>
<tr>
<td>Students Name</td>
<td>
<input type="hidden" name="id" value="<?php echo $row["id"]; ?>">
<input type="text" name="studname" value="<?php echo $row["studname"]; ?>" id="">
</td>
</tr>
<tr>
<td>Students course</td>
<td>
<select name="course" id="">
<option value="bca" <?php echo $row["course"]=="bca"?"selected":"" ?>>BCA</option>
<option value="bscit" <?php echo $row["course"]=="bscit"?"selected":"" ?>>BSCIT</option>
<option value="bcom" <?php echo $row["course"]=="bcom"?"selected":"" ?>>BCOM</option>
<option value="mcom" <?php echo $row["course"]=="mcom"?"selected":"" ?>>MCOM</option>
</select>
</td>
</tr>
<tr>
<td>Students City</td>
<td><input type="text" name="city" id="" value="<?php echo $row["city"]; ?>"></td>
</tr>
<tr>
<td>Students Age</td>
<td><input type="text" name="age" id="" value="<?php echo $row["age"]; ?>"></td>
</tr>
<tr>
<td>Students Mobile</td>
<td><input type="text" name="mobile" id="" value="<?php echo $row["mobile"]; ?>"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="UPDATE">
</td>
</tr>
</table>
</form>
</body>
</html>
students_update.php
<?php
extract($_POST);
$conn=mysqli_connect("localhost","root","","hvc");
if(!$conn){
echo "connection failed";
}else{
echo "connection success";
$qry="update students set studname='$studname',course='$course',city='$city', age='$age'
, mobile='$mobile' where id='$id'";
mysqli_query($conn,$qry);
header("location:index.php");
}
?>