PHP CRUD (Create, Read, Update, Delete) | PHP Tutorial - Learn with VOKS
Back Next

PHP CRUD (Create, Read, Update, Delete)


Prerequisites

  1. Web Server – XAMPP, WAMP, MAMP, or LAMP
  2. PHP Installed – PHP 7+ recommended
  3. 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 identifier
  • name → User’s name
  • email → 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 data
  • INSERT 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 array
  • num_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 query
  • UPDATE → 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

  1. Create: Form submits new user → INSERT INTO
  2. Read: Display users → SELECT *
  3. Update: Edit user → UPDATE
  4. Delete: Remove user → DELETE
This forms the basic structure of dynamic web applications like blogs, user management, and e-commerce.

Key Points:

  1. CRUD is essential for database-driven web apps.
  2. Use INSERT, SELECT, UPDATE, DELETE SQL commands.
  3. Always validate and sanitize user input to prevent SQL injection.
  4. PHP + MySQL + HTML forms = dynamic, functional web apps.
  5. You can integrate Bootstrap to make the interface user-friendly.

Practice Example: Simple PHP CRUD Table


Example Code:
//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");
?>
PHP
Introduction PHP Configuration PHP CRUD (Create, Read, Update, Delete) PHP Error Handling PHP Form Submission Login System PHP Comments
All Courses
Advance AI Bootstrap C C++ Computer Vision Content Writing CSS Cyber Security Data Analysis Deep Learning Email Marketing Excel Figma HTML Java Script Machine Learning MySQLi Node JS PHP Power Bi Python Python for AI Python for Analysis React React Native SEO SMM SQL