New! Explore our Programming Academy and AI Tutor - learn to code from scratch, free to start. Explore Now
Programming PHP Build a Blog System

Build a Blog System

🐘 PHP

Project: build a simple blog with posts, create, read, update, and delete functionality.

Lesson 5 of 5 Project
0/5 completed

Project: Blog System

Build a simple blog with CRUD functionality.

Requirements

  1. Create a posts table (id, title, body, created_at)
  2. List all posts on the home page
  3. Create new posts with a form
  4. View individual posts
  5. Delete posts

Bonus Features

  • Edit existing posts
  • Add categories/tags
  • Add author names
  • Pagination

Example

<?php
// Simple Blog — index.php
$pdo = new PDO("mysql:host=localhost;dbname=blog", "root", "", [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);

// Handle create
if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["title"])) {
    $stmt = $pdo->prepare("INSERT INTO posts (title, body) VALUES (?, ?)");
    $stmt->execute([$_POST["title"], $_POST["body"]]);
    header("Location: index.php");
    exit;
}

// Handle delete
if (isset($_GET["delete"])) {
    $stmt = $pdo->prepare("DELETE FROM posts WHERE id = ?");
    $stmt->execute([$_GET["delete"]]);
    header("Location: index.php");
    exit;
}

$posts = $pdo->query("SELECT * FROM posts ORDER BY id DESC")->fetchAll();
?>
<!DOCTYPE html>
<html>
<head><title>My Blog</title></head>
<body>
<h1>My Blog</h1>
<form method="POST">
    <input type="text" name="title" placeholder="Title" required>
    <textarea name="body" placeholder="Content…" required></textarea>
    <button type="submit">Post</button>
</form>
<hr>
<?php foreach ($posts as $post): ?>
    <h3><?= htmlspecialchars($post["title"]) ?></h3>
    <p><?= nl2br(htmlspecialchars($post["body"])) ?></p>
    <a href="?delete=<?= $post["id"] ?>" onclick="return confirm('Delete?')">Delete</a>
    <hr>
<?php endforeach; ?>
</body>
</html>
🚀 Project

Build a Blog System

Project: build a simple blog with posts, create, read, update, and delete functionality.

Requirements
  • 1. A posts table (id, title, body, created_at)
  • 2. List all posts on the home page
  • 3. Create new posts with a form
  • 4. View individual posts
  • 5. Delete posts
  • 6. Use prepared statements to prevent SQL injection
🚀 Start Project

Lesson Nav

Lesson sections