Step:1 Create a Database in MySQL “bca”
Step:2 Create a table “students”
Fields:
Id : INT : PRIMARY KEY
Sname: varchar(30)
City: varchar(30)
index.html
<html>
<head>
<title>Form</title>
</head>
<body>
<form action="InsertRec.php" method="post">
Name : <input type="text"
name="txtName"><br>
City : <input type="text"
name="txtCity"><br>
<input type="Submit" value = "Insert">
<input type="Reset" value ="Clear All">
</form>
</body>
</html>
InsertRec.php
<?php
$sname = trim($_POST["txtName"]);
$city = trim($_POST["txtCity"]);
if ($name === "")
{ // When No Name is Entered - It will not insert record
header("Location: index.html");
exit(); // Stop further execution
}
$con = mysqli_connect("localhost", "root", "", "bca");
$stmt = mysqli_query($con,"INSERT INTO students
(sname, city) VALUES ('$sname','$city')");
header("Location: DisplayRecords.php");
?>
DisplayRecords.php
<html>
<head>
<title>Form</title>
</head>
<body>
<table border="4">
<tr>
<th>Id</th>
<th>Name</th>
<th>City</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<?php
$con = mysqli_connect(
"localhost",
"root",
"", "bca");
$qry = "SELECT * FROM students";
$s = mysqli_query($con, $qry);
while ($r = mysqli_fetch_row($s))
{
echo "<tr>";
echo "<td>".$r[0]."</td>";
echo"<td>".$r[1]."</td>";
echo "<td>".$r[2]."</td>";
echo "<td><a href='EditRecord.php?id=".
$r[0]."'>Edit</a></td>";
echo "<td><a
href='DeleteRecord.php?id=".
$r[0]."'>Delete</a></td>";
echo "</tr>";
}
echo "<tr>
<a href='index.html'>
Insert More Records
</a>
</tr>";
mysqli_close($con);
?>
</table>
</body>
</html>
DeleteRecord.php
(Here, SURAJ record is deleted)
<?php
$id = $_REQUEST["id"];
$con = mysqli_connect("localhost",
"root", "", "bca");
$stmt = mysqli_query($con,
"DELETE FROM students WHERE id = $id");
header("Location: DisplayRecords.php");
?>
EditRecord.php
<?php
$id = $_REQUEST["id"];
$con = mysqli_connect("localhost", "root", "", "bca");
$stmt =mysqli_query($con,
"SELECT * FROM students WHERE id = $id");
$rec = mysqli_fetch_array($stmt);
?>
<form action="Save.php" method="post">
<input type="hidden" name="id"
value="<?php echo $rec[0]; ?>"><br>
Name: <input type="text" name="txtName"
value="<?php echo $rec[1]; ?>"><br>
City: <input type="text" name="txtCity"
value="<?php echo $rec[2]; ?>"><br>
<input type="submit"> <input type="reset">
</form>
Save.php
<?php
$id = $_POST["id"];
$sname= $_POST["txtName"];
$city = $_POST["txtCity"];
$con = mysqli_connect("localhost",
"root", "", "bca");
$stmt = mysqli_query($con,
"UPDATE students
SET sname = '$sname',
city = '$city'
WHERE id = $id");
header("Location: DisplayRecords.php");
?>

"PHP and MySQL CRUD Operations for Student Management System"

  • 1.
    Step:1 Create aDatabase in MySQL “bca” Step:2 Create a table “students” Fields: Id : INT : PRIMARY KEY Sname: varchar(30) City: varchar(30)
  • 2.
    index.html <html> <head> <title>Form</title> </head> <body> <form action="InsertRec.php" method="post"> Name: <input type="text" name="txtName"><br> City : <input type="text" name="txtCity"><br> <input type="Submit" value = "Insert"> <input type="Reset" value ="Clear All"> </form> </body> </html> InsertRec.php <?php $sname = trim($_POST["txtName"]); $city = trim($_POST["txtCity"]); if ($name === "") { // When No Name is Entered - It will not insert record header("Location: index.html"); exit(); // Stop further execution } $con = mysqli_connect("localhost", "root", "", "bca");
  • 3.
    $stmt = mysqli_query($con,"INSERTINTO students (sname, city) VALUES ('$sname','$city')"); header("Location: DisplayRecords.php"); ?> DisplayRecords.php <html> <head> <title>Form</title> </head> <body> <table border="4"> <tr> <th>Id</th> <th>Name</th> <th>City</th> <th>Edit</th> <th>Delete</th> </tr> <?php $con = mysqli_connect( "localhost", "root", "", "bca"); $qry = "SELECT * FROM students"; $s = mysqli_query($con, $qry); while ($r = mysqli_fetch_row($s)) { echo "<tr>"; echo "<td>".$r[0]."</td>"; echo"<td>".$r[1]."</td>"; echo "<td>".$r[2]."</td>"; echo "<td><a href='EditRecord.php?id=". $r[0]."'>Edit</a></td>"; echo "<td><a href='DeleteRecord.php?id=".
  • 4.
    $r[0]."'>Delete</a></td>"; echo "</tr>"; } echo "<tr> <ahref='index.html'> Insert More Records </a> </tr>"; mysqli_close($con); ?> </table> </body> </html> DeleteRecord.php (Here, SURAJ record is deleted) <?php $id = $_REQUEST["id"]; $con = mysqli_connect("localhost", "root", "", "bca"); $stmt = mysqli_query($con, "DELETE FROM students WHERE id = $id"); header("Location: DisplayRecords.php"); ?> EditRecord.php <?php $id = $_REQUEST["id"]; $con = mysqli_connect("localhost", "root", "", "bca"); $stmt =mysqli_query($con,
  • 5.
    "SELECT * FROMstudents WHERE id = $id"); $rec = mysqli_fetch_array($stmt); ?> <form action="Save.php" method="post"> <input type="hidden" name="id" value="<?php echo $rec[0]; ?>"><br> Name: <input type="text" name="txtName" value="<?php echo $rec[1]; ?>"><br> City: <input type="text" name="txtCity" value="<?php echo $rec[2]; ?>"><br> <input type="submit"> <input type="reset"> </form> Save.php <?php $id = $_POST["id"]; $sname= $_POST["txtName"]; $city = $_POST["txtCity"]; $con = mysqli_connect("localhost", "root", "", "bca"); $stmt = mysqli_query($con, "UPDATE students SET sname = '$sname', city = '$city' WHERE id = $id");
  • 6.