Day 9 – CRUD Operations in PHP MySQL Step-by-Step

Introduction

On Day 9, we move forward from database integration to CRUD operations in PHP with MySQL. CRUD stands for Create, Read, Update, and Delete — the fundamental operations that allow a web application to manage data dynamically.

At CuriosityTech.in, learners not only understand the theory but implement real-world CRUD systems, such as user management dashboards, product catalogs, and content management systems, ensuring hands-on experience in Full Stack development.


1. Setting Up the Database Table

Before performing CRUD operations, you need a MySQL table. For example, a users table:

CREATE TABLE users (

    id INT AUTO_INCREMENT PRIMARY KEY,

    name VARCHAR(100) NOT NULL,

    email VARCHAR(100) UNIQUE NOT NULL,

    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP

);

Table Structure:

ColumnData TypeConstraint
idINTPRIMARY KEY, AUTO_INCREMENT
nameVARCHAR(100)NOT NULL
emailVARCHAR(100)UNIQUE, NOT NULL
created_atTIMESTAMPDEFAULT CURRENT_TIMESTAMP

2. Create – Inserting Data

Using PDO for secure data insertion:

<?php

$name = “Bhavesh Barange”;

$email = “bhavesh@curiositytech.in”;

$stmt = $pdo->prepare(“INSERT INTO users (name, email) VALUES (:name, :email)”);

$stmt->bindParam(‘:name’, $name);

$stmt->bindParam(‘:email’, $email);

$stmt->execute();

echo “User added successfully!”;

?>

Tip: Always use prepared statements to prevent SQL injection.


3. Read – Retrieving Data

Fetching data allows you to display database records dynamically:

<?php

$stmt = $pdo->query(“SELECT id, name, email FROM users”);

echo “<table border=’1′>

<tr><th>ID</th><th>Name</th><th>Email</th></tr>”;

while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

    echo “<tr>

            <td>{$row[‘id’]}</td>

            <td>{$row[‘name’]}</td>

            <td>{$row[’email’]}</td>

          </tr>”;

}

echo “</table>”;

?>

Output:
 A dynamic HTML table showing all users in the database.


4. Update – Modifying Records

Updating records is crucial for managing user data:

<?php

$id = 1;

$newName = “Bhavesh Updated”;

$stmt = $pdo->prepare(“UPDATE users SET name = :name WHERE id = :id”);

$stmt->bindParam(‘:name’, $newName);

$stmt->bindParam(‘:id’, $id);

$stmt->execute();

echo “User updated successfully!”;

?>

Key Points:

  • Always validate user input before updating records.

  • Use unique identifiers (like id) to ensure precise updates.


5. Delete – Removing Records

Deleting records permanently from the database:

<?php

$id = 1;

$stmt = $pdo->prepare(“DELETE FROM users WHERE id = :id”);

$stmt->bindParam(‘:id’, $id);

$stmt->execute();

echo “User deleted successfully!”;

?>

Security Tip:

  • Confirm deletion actions with users to avoid accidental data loss.

  • Use prepared statements to prevent malicious queries.


6. CRUD Hierarchy Diagram


7. Best Practices for CRUD in PHP

PracticeRecommendation
SecurityUse prepared statements (PDO/MySQLi)
ValidationAlways validate user input before DB operations
Error HandlingCatch exceptions and log errors
ModularityUse functions or classes for repeated CRUD logic
User ExperienceProvide confirmation messages for update/delete actions

8. CuriosityTech.in Perspective

At CuriosityTech.in, students practice CRUD operations on real projects like contact forms, online stores, and content management systems. By combining PHP core skills (Day 6), secure form handling (Day 7), and database integration (Day 8), learners gain the ability to build dynamic, functional, and secure full-stack applications ready for production.


Conclusion

CRUD operations form the backbone of any dynamic web application. Mastering Create, Read, Update, and Delete in PHP with MySQL allows developers to manage data effectively, implement complex business logic, and ensure application scalability. Today’s skills are the foundation for authentication and session management in Day 10.


Leave a Comment

Your email address will not be published. Required fields are marked *