Prerequisites
Database Setup Example:
CREATE DATABASE testdb;
USE testdb;
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL
);
id → Unique identifiername → User’s nameemail → User’s emailDatabase Connection (config.php)
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "testdb";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
}
?>
$conn → Connection object used for all database queriesCREATE Operation (create.php)
<?php
include 'config.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST['name'];
$email = $_POST['email'];
$sql = "INSERT INTO users (name, email) VALUES ('$name', '$email')";
mysqli_query($conn, $sql);
header("Location: read.php");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Create User</title>
</head>
<body>
<form method="POST">
Name: <input type="text" name="name" required><br><br>
Email: <input type="email" name="email" required><br><br>
<input type="submit" value="Create">
</form>
</body>
</html>
$_POST → Retrieves form dataINSERT INTO → SQL query to add data⚠️ Tip: For security, use prepared statements to prevent SQL injection.
READ Operation (read.php)
<?php
include 'config.php';
$result = $conn->query("SELECT * FROM users");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Read Users</title>
</head>
<body>
<h2>Users</h2>
<a href="create.php">Add New User</a>
<table border="1" cellpadding="10">
<tr>
<th>ID</th><th>Name</th><th>Email</th><th>Actions</th>
</tr>
<?php while($row = $result->fetch_assoc()): ?>
<tr>
<td><?= $row['id'] ?></td>
<td><?= $row['name'] ?></td>
<td><?= $row['email'] ?></td>
<td>
<a href="update.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>
$result->fetch_assoc() → Fetch each row as an associative arraynum_rows → Number of rows returnedUPDATE Operation (update.php)
<?php
include 'config.php';
$id = $_GET['id'];
$result = $conn->query("SELECT * FROM users WHERE id=$id");
$user = $result->fetch_assoc();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
$email = $_POST['email'];
$conn->query("UPDATE users SET name='$name', email='$email' WHERE id=$id");
header("Location: read.php");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Update User</title>
</head>
<body>
<h2>Update User</h2>
<form method="post" >
Name: <input type="text" name="name" value="<?= $user['name'] ?>" required><br><br>
Email: <input type="email" name="email" value="<?= $user['email'] ?>" required><br><br>
<input type="submit" value="Update">
</form>
</body>
</html>
$_GET['id'] → Gets user ID from URL queryUPDATE → SQL command to modify data<td> <a href="edit.php?id=<?= $row['id'] ?>">Edit</a> </td>
DELETE Operation (delete.php)
<?php
include 'config.php';
$id = $_GET['id'];
$conn->query("DELETE FROM users WHERE id=$id");
header("Location: create.php");
?>
DELETE FROM → Removes the record<td>
<a href="delete.php?id=<?= $row['id'] ?>" onclick="return confirm('Are you sure?')">Delete</a>
</td>
Complete CRUD Flow
INSERT INTOSELECT *UPDATEDELETEThis forms the basic structure of dynamic web applications like blogs, user management, and e-commerce.
✅ Key Points:
INSERT, SELECT, UPDATE, DELETE SQL commands.//Create database table
CREATE DATABASE testdb;
USE testdb;
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL
);
//Database configuration (config.php)
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "testdb";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
}
?>
//Create Operation (create.php)
<?php
include 'config.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST['name'];
$email = $_POST['email'];
$sql = "INSERT INTO users (name, email) VALUES ('$name', '$email')";
mysqli_query($conn, $sql);
header("Location: read.php");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Create User</title>
</head>
<body>
<form method="POST">
Name: <input type="text" name="name" required><br><br>
Email: <input type="email" name="email" required><br><br>
<input type="submit" value="Create">
</form>
</body>
</html>
//Read Operation (read.php)
<?php
include 'config.php';
$result = $conn->query("SELECT * FROM users");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Read Users</title>
</head>
<body>
<h2>Users</h2>
<a href="create.php">Add New User</a>
<table border="1" cellpadding="10">
<tr>
<th>ID</th><th>Name</th><th>Email</th><th>Actions</th>
</tr>
<?php while($row = $result->fetch_assoc()): ?>
<tr>
<td><?= $row['id'] ?></td>
<td><?= $row['name'] ?></td>
<td><?= $row['email'] ?></td>
<td>
<a href="update.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>
//Update Operation (update.php)
<?php
include 'config.php';
$id = $_GET['id'];
$result = $conn->query("SELECT * FROM users WHERE id=$id");
$user = $result->fetch_assoc();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
$email = $_POST['email'];
$conn->query("UPDATE users SET name='$name', email='$email' WHERE id=$id");
header("Location: read.php");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Update User</title>
</head>
<body>
<h2>Update User</h2>
<form method="post" >
Name: <input type="text" name="name" value="<?= $user['name'] ?>" required><br><br>
Email: <input type="email" name="email" value="<?= $user['email'] ?>" required><br><br>
<input type="submit" value="Update">
</form>
</body>
</html>
//Delete Operation (delete.php)
<?php
include 'config.php';
$id = $_GET['id'];
$conn->query("DELETE FROM users WHERE id=$id");
header("Location: create.php");
?>