Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 17

PHP CRUD Operations with MySQL (Full Code)

In this lesson: Build a complete PHP MySQL CRUD app with PDO — insert, display, edit and delete records — full source code with steps.

What is CRUD

CRUD stands for Create, Read, Update, Delete — the four things you do with stored data. A blog creates posts, reads them to show, updates them when edited, and deletes them when removed. Master these four and you can build almost any data-driven feature.

💡 CRUD maps to four SQL commands: INSERT, SELECT, UPDATE, DELETE.

The table and connection

We use a simple students table and a PDO connection.

<?php
  $pdo = new PDO("mysql:host=localhost;dbname=school", "root", "");
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  // table: students(id, name, marks)
?>

All four operations below use this $pdo connection and prepared statements to stay safe from SQL injection.

Create (INSERT)

<?php
  $sql = "INSERT INTO students (name, marks) VALUES (?, ?)";
  $stmt = $pdo->prepare($sql);
  $stmt->execute(["Aman", 88]);
  echo "Created row";
?>
Created row

The ? placeholders receive the real values from execute(), keeping user data out of the SQL text.

Read (SELECT)

<?php
  $stmt = $pdo->query("SELECT id, name, marks FROM students");
  $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  foreach ($rows as $r) {
      echo $r["name"] . " - " . $r["marks"] . "\n";
  }
?>
Aman - 88

fetchAll(PDO::FETCH_ASSOC) gives every row as an array keyed by column name, so you read $r["name"] directly.

Update (UPDATE)

<?php
  $sql = "UPDATE students SET marks = ? WHERE id = ?";
  $stmt = $pdo->prepare($sql);
  $stmt->execute([95, 1]);
  echo "Updated row 1";
?>
Updated row 1

The WHERE id = ? is essential — it limits the change to one student. Without it, every student's marks would change.

Delete (DELETE)

<?php
  $sql = "DELETE FROM students WHERE id = ?";
  $stmt = $pdo->prepare($sql);
  $stmt->execute([1]);
  echo "Deleted row 1";
?>
Deleted row 1

Again the WHERE clause protects you — deleting without it would empty the whole table. Always target the exact record.

Common mistakes

  • No WHERE on UPDATE/DELETE. This changes or deletes every row — always target one record.
  • Pasting input into SQL. Use placeholders and execute() to prevent injection.
  • Ignoring errors. Set ERRMODE_EXCEPTION and wrap risky code in try-catch.
  • Forgetting FETCH_ASSOC. Set it so rows are readable by column name.

Summary

  • CRUD = Create, Read, Update, Delete — the four core data operations.
  • They map to INSERT, SELECT, UPDATE and DELETE.
  • Use PDO prepared statements for every operation to block SQL injection.
  • Always add WHERE to UPDATE and DELETE.
  • Read results with fetchAll(PDO::FETCH_ASSOC).

Next, connect and secure data with PHP MySQL connection, or build a real app with login and registration.

Frequently Asked Questions

What does CRUD stand for?
CRUD stands for Create, Read, Update and Delete — the four basic operations you perform on data. Almost every app that stores information (users, products, posts) is built around these four actions.
Which SQL command matches each CRUD operation?
Create uses INSERT, Read uses SELECT, Update uses UPDATE, and Delete uses DELETE. Learning CRUD means learning these four SQL statements and how to run them safely from PHP.
Why use prepared statements for CRUD?
CRUD usually involves user data, and pasting that straight into SQL invites SQL injection. Prepared statements send the values separately from the query, so user input can never be run as SQL. Use them for every CRUD operation.
Why must UPDATE and DELETE have a WHERE clause?
Without WHERE, an UPDATE changes every row and a DELETE removes every row in the table. The WHERE clause limits the action to the specific record you mean, preventing accidental mass changes.
How do I read the results of a SELECT in PHP?
After executing the statement, use fetch() for one row or fetchAll(PDO::FETCH_ASSOC) for all rows. Then loop through them and read each column by its name.
← Back to PHP Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicPHP Interview Questions Your progress: 17 / 50 (34%) · Lesson 18 / 50 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free