PHPPHP20 min read

CRUD in PHP with PDO (Create, Read, Update, Delete)

Build a full CRUD flow with PDO and a clean folder structure you can reuse.

Lauren James
September 23, 2025
6.4k180

CRUD is the backbone of most apps. Let’s build it in a clean way.

Example table: posts

CREATE TABLE posts (
  id INT AUTO_INCREMENT PRIMARY KEY,
  title VARCHAR(200) NOT NULL,
  body TEXT NOT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

Create (INSERT)

$stmt = $pdo->prepare("INSERT INTO posts (title, body) VALUES (:title, :body)");
$stmt->execute(["title" => $title, "body" => $body]);

Read (SELECT all)

$rows = $pdo->query("SELECT * FROM posts ORDER BY id DESC")->fetchAll();

Update

$stmt = $pdo->prepare("UPDATE posts SET title=:title, body=:body WHERE id=:id");
$stmt->execute(["title" => $title, "body" => $body, "id" => $id]);

Delete

$stmt = $pdo->prepare("DELETE FROM posts WHERE id=:id");
$stmt->execute(["id" => $id]);

Tip for real projects

Wrap DB logic in functions or a small class so pages stay clean.

Next: Password hashing, secure login foundations.

#PHP#PDO#CRUD#Intermediate