Build a Blog System
🐘 PHPProject: build a simple blog with posts, create, read, update, and delete functionality.
0/5 completed
Project: Blog System
Build a simple blog with CRUD functionality.
Requirements
- Create a
poststable (id, title, body, created_at) - List all posts on the home page
- Create new posts with a form
- View individual posts
- 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
🚀 Start Project
- 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