Prerequisites
- Web Server – XAMPP, WAMP, MAMP, or LAMP
- PHP Installed – PHP 7+ recommended
- MySQL Database
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 email
Database 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 queries
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>
$_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 returned
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>
$_GET['id']→ Gets user ID from URL queryUPDATE→ SQL command to modify data- Usually called from a link/button with the ID in the URL:
<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- Usually called from a link/button with the ID in the URL:
<td>
<a href="delete.php?id=<?= $row['id'] ?>" onclick="return confirm('Are you sure?')">Delete</a>
</td>
Complete CRUD Flow
- Create: Form submits new user →
INSERT INTO - Read: Display users →
SELECT * - Update: Edit user →
UPDATE - Delete: Remove user →
DELETE
This forms the basic structure of dynamic web applications like blogs, user management, and e-commerce.
✅ Key Points:
- CRUD is essential for database-driven web apps.
- Use
INSERT,SELECT,UPDATE,DELETESQL commands. - Always validate and sanitize user input to prevent SQL injection.
- PHP + MySQL + HTML forms = dynamic, functional web apps.
- You can integrate Bootstrap to make the interface user-friendly.
Practice Example: Simple PHP CRUD Table
//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");
?>