Full CRUD Application using HTML, PHP
& MySQLi
Database Setup (MySQL)
CREATE DATABASE crud_example;
USE crud_example;
CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
age INT NOT NULL,
email VARCHAR(100)
);
db.php
<?php
$host = "localhost";
$user = "root";
$pass = "";
$dbname = "crud_example";
$conn = new mysqli($host, $user, $pass, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
index.php – READ
<?php include 'db.php'; ?>
<!DOCTYPE html>
<html>
<head><title>Student List</title></head>
<body>
<h2>Student List</h2>
<a href="create.php">Add New Student</a>
<table border="1" cellpadding="5">
<tr><th>ID</th><th>Name</th><th>Age</th><th>Email</th><th>Actions</
th></tr>
<?php
$result = $conn->query("SELECT * FROM students");
while ($row = $result->fetch_assoc()):
?>
<tr>
<td><?= $row['id']; ?></td>
<td><?= $row['name']; ?></td>
<td><?= $row['age']; ?></td>
<td><?= $row['email']; ?></td>
<td>
<a href="edit.php?id=<?= $row['id']; ?>">Edit</a> |
<a href="delete.php?id=<?= $row['id']; ?>" onclick="return
confirm('Are you sure?')">Delete</a>
</td>
</tr>
<?php endwhile; ?>
</table>
</body>
</html>
create.php – CREATE
<?php include 'db.php'; ?>
<!DOCTYPE html>
<html>
<head><title>Add Student</title></head>
<body>
<h2>Add Student</h2>
<form method="POST">
Name: <input type="text" name="name" required><br><br>
Age: <input type="number" name="age" required><br><br>
Email: <input type="email" name="email"><br><br>
<input type="submit" name="save" value="Save">
</form>
<?php
if (isset($_POST['save'])) {
$name = $_POST['name'];
$age = $_POST['age'];
$email = $_POST['email'];
$conn->query("INSERT INTO students (name, age, email) VALUES
('$name', $age, '$email')");
echo "Student added successfully! <a href='index.php'>Back to
list</a>";
}
?>
</body>
</html>
edit.php – UPDATE
<?php
include 'db.php';
$id = $_GET['id'];
$result = $conn->query("SELECT * FROM students WHERE id=$id");
$row = $result->fetch_assoc();
?>
<!DOCTYPE html>
<html>
<head><title>Edit Student</title></head>
<body>
<h2>Edit Student</h2>
<form method="POST">
Name: <input type="text" name="name" value="<?= $row['name']; ?>"
required><br><br>
Age: <input type="number" name="age" value="<?= $row['age']; ?>"
required><br><br>
Email: <input type="email" name="email" value="<?= $row['email']; ?
>"><br><br>
<input type="submit" name="update" value="Update">
</form>
<?php
if (isset($_POST['update'])) {
$name = $_POST['name'];
$age = $_POST['age'];
$email = $_POST['email'];
$conn->query("UPDATE students SET name='$name', age=$age,
email='$email' WHERE id=$id");
echo "Student updated! <a href='index.php'>Back to list</a>";
}
?>
</body>
</html>
delete.php – DELETE
<?php
include 'db.php';
$id = $_GET['id'];
$conn->query("DELETE FROM students WHERE id=$id");
header("Location: index.php");
exit;
?>