CRUD Operations

Introduction

CRUD stands for Create, Read, Update, and Delete. These are the four basic operations used in most database-driven applications. CRUD operations allow users to manage data efficiently in websites and web applications.

CRUD is commonly used in PHP, MySQL, Laravel, Node.js, and other web technologies to build dynamic systems such as student management systems, e-commerce websites, inventory systems, and content management platforms.

Objectives

By the end of this training, you will be able to:

  • Understand the concept of CRUD operations
  • Create new records in a database
  • Read and display database records
  • Update existing records
  • Delete records safely
  • Connect PHP with MySQL database
  • Build a simple CRUD application

Understanding CRUD Operations

Create

The Create operation is used to insert new data into a database.

Examples:

  • Registering a new user
  • Adding a product
  • Saving student information

Read

The Read operation retrieves and displays data from a database.

Examples:

  • Viewing products
  • Displaying user profiles
  • Showing reports and records

Update

The Update operation modifies existing data.

Examples:

  • Editing profile information
  • Updating product prices
  • Changing passwords

Delete

The Delete operation removes data from a database.

Examples:

  • Deleting a user account
  • Removing products
  • Clearing old records

Requirements for CRUD Application

To build CRUD applications, you need:

  • PHP
  • MySQL Database
  • XAMPP or WAMP
  • Text Editor or IDE
  • Web Browser

Database Creation

Create Database

CREATE DATABASE crud_app;

Create Table

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

Database Connection in PHP

<?php

$conn = mysqli_connect("localhost", "root", "", "crud_app");

if (!$conn) {
die("Connection Failed");
}

?>

CREATE Operation

The Create operation inserts data into the database.

HTML Form

<form method="POST">
<input type="text" name="name" placeholder="Enter Name">
<input type="email" name="email" placeholder="Enter Email">
<input type="text" name="course" placeholder="Enter Course">
<button type="submit" name="save">Save</button>
</form>

Insert Data Using PHP

<?php

if(isset($_POST['save'])) {

$name = $_POST['name'];
$email = $_POST['email'];
$course = $_POST['course'];

$query = "INSERT INTO students(name, email, course)
VALUES('$name', '$email', '$course')";

mysqli_query($conn, $query);

echo "Data Inserted Successfully";
}

?>

READ Operation

The Read operation fetches and displays records from the database.

<?php

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

while($row = mysqli_fetch_assoc($result)) {

echo $row['id'];
echo $row['name'];
echo $row['email'];
echo $row['course'];
}

?>

UPDATE Operation

The Update operation edits existing records.

<?php

$id = 1;

$query = "UPDATE students
SET name='Ali Khan'
WHERE id='$id'";

mysqli_query($conn, $query);

echo "Record Updated Successfully";

?>

DELETE Operation

The Delete operation removes records from the database.

<?php

$id = 1;

$query = "DELETE FROM students WHERE id='$id'";

mysqli_query($conn, $query);

echo "Record Deleted Successfully";

?>

Complete CRUD Workflow

Step 1

Create the database and table.

Step 2

Connect PHP with MySQL.

Step 3

Create forms for data input.

Step 4

Insert records into the database.

Step 5

Display records using SELECT query.

Step 6

Update existing records.

Step 7

Delete unwanted records.

Advantages of CRUD Operations

  • Simplifies database management
  • Makes applications dynamic
  • Improves data handling efficiency
  • Supports real-time data updates
  • Essential for web development projects

Applications of CRUD

CRUD operations are used in:

  • Student Management Systems
  • Hospital Management Systems
  • E-commerce Websites
  • Inventory Systems
  • Employee Management Systems
  • Blogging Platforms

Best Practices

  • Validate user input
  • Use prepared statements for security
  • Prevent SQL injection
  • Confirm before deleting records
  • Use meaningful database structure
  • Keep backups of important data

Career Opportunities

Learning CRUD operations can help you become:

  • PHP Developer
  • Backend Developer
  • Full Stack Developer
  • Database Administrator
  • Web Application Developer

Final Presentation

In your final presentation, explain:

  • What CRUD operations are
  • Importance of CRUD in web development
  • Difference between Create, Read, Update, and Delete
  • Database connection process
  • Real-world examples of CRUD applications
  • Benefits of learning CRUD operations
Home Ā» Advanced PHP > MySQL Database > CRUD Operations