CRUD Application in PHP

Introduction

A CRUD Application in PHP allows developers to perform four essential database operations: Create, Read, Update, and Delete. These operations form the backbone of dynamic websites and web applications.

PHP, combined with MySQL, is widely used to build CRUD applications due to its simplicity, flexibility, and strong database support. In this article, we will understand what a CRUD application is, why it is used, its syntax, examples, and best practices.

What is a CRUD Application in PHP?

A CRUD Application in PHP is a system that performs four main operations on a database:

  • Create → Insert new data into the database
  • Read → Fetch and display data
  • Update → Modify existing data
  • Delete → Remove data from the database

These operations are used in almost every web application.

For example:

  • Creating a user account → Create
  • Viewing user profile → Read
  • Editing profile → Update
  • Deleting account → Delete

Syntax

1. Database Table Example


CREATE TABLE students (
   id INT AUTO_INCREMENT PRIMARY KEY,
   name VARCHAR(100),
   email VARCHAR(100),
   course VARCHAR(50)
);

2. Create (Insert Data)


$sql = "INSERT INTO students (name, email, course)
       VALUES ('$name', '$email', '$course')";
mysqli_query($conn, $sql);

3. Read (Fetch Data)


$sql = "SELECT * FROM students";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result)){
   echo $row['name'];
}

4. Update (Modify Data)


$sql = "UPDATE students SET name='$name' WHERE id=$id";
mysqli_query($conn, $sql);

5. Delete (Remove Data)


$sql = "DELETE FROM students WHERE id=$id";
mysqli_query($conn, $sql);

Example:

Let’s create a simple CRUD application step by step.

Step 1: Database Connection (config.php)


$conn = mysqli_connect("localhost", "root", "", "test_db");
if(!$conn){
   die("Connection Failed: " . mysqli_connect_error());
}

Step 2: Create Form (Add Data)


<form method="POST" action="insert.php">
   <input type="text" name="name" placeholder="Enter Name" required>
   <input type="email" name="email" placeholder="Enter Email" required>
   <input type="text" name="course" placeholder="Enter Course" required>
   <button type="submit">Add Student</button>
</form>

Step 3: Insert Data (insert.php)


include "config.php";
$name = $_POST['name'];
$email = $_POST['email'];
$course = $_POST['course'];
$sql = "INSERT INTO students (name, email, course)
       VALUES ('$name', '$email', '$course')";
if(mysqli_query($conn, $sql)){
   echo "Data Inserted Successfully";
} else {
   echo "Error";
}

Step 4: Display Data (index.php)


<?php
include "config.php";

$result = mysqli_query($conn, "SELECT * FROM students");

while($row = mysqli_fetch_assoc($result)){
?>
   <p>
       <?php echo $row['name']; ?>
       <a href="edit.php?id=<?php echo $row['id']; ?>">Edit</a>
       <a href="delete.php?id=<?php echo $row['id']; ?>">Delete</a>
   </p>
<?php
}
?>

Step 5: Update Data (edit.php)


<?php
include "config.php";

$id = $_GET['id'];

if($_SERVER['REQUEST_METHOD'] == 'POST'){
   $name = $_POST['name'];
   mysqli_query($conn, "UPDATE students SET name='$name' WHERE id=$id");
   header("Location: index.php");
}

$result = mysqli_query($conn, "SELECT * FROM students WHERE id=$id");
$row = mysqli_fetch_assoc($result);
?>

<form method="POST">
   <input type="text" name="name" value="<?php echo $row['name']; ?>">
   <button type="submit">Update</button>
</form>

Step 6: Delete Data (delete.php)


include "config.php";
$id = $_GET['id'];
mysqli_query($conn, "DELETE FROM students WHERE id=$id");
header("Location: index.php");

Real-Life Example

CRUD applications are used everywhere:

1. Blog Website

  • Create → Add new post
  • Read → View posts
  • Update → Edit post
  • Delete → Remove post

2. E-commerce Website

  • Add products
  • View product list
  • Update product details
  • Delete products

3. Student Management System

  • Add student
  • View student data
  • Update details
  • Delete records

For example, an admin dashboard uses CRUD to manage all data efficiently.

Common Mistakes

1. Not Using Prepared Statements

❌ Direct queries can lead to SQL Injection.

✔️ Use prepared statements for security.

2. No Input Validation

Allowing invalid data can break the system.

3. Not Handling Errors

Always check if queries are successful.

4. No Confirmation Before Delete

Deleting data without confirmation can cause accidental loss.

5. Poor UI/UX

A bad interface makes CRUD operations difficult to use.

6. Not Securing IDs

Using direct IDs in URLs without validation can be risky.

Conclusion

A CRUD Application in PHP is a fundamental concept. It enables efficient data management through Create, Read, Update, and Delete operations.

Mastering CRUD operations will help you build real-world applications like admin dashboards, content management systems, and complete web platforms.

Related PHP Tutorials