[PHP Tutorial] Building a Forum with CRUD Operations (Create, Read, Update, Delete)
![[PHP Tutorial] Building a Forum with CRUD Operations (Create, Read, Update, Delete)](/uploads/editor/PHP-강좌-10강-게시판-글쓰기-목록-수정-삭제-CRUD-만들기-20260703-122563.webp)
![[PHP 강의] PHP 8.4 웹 프로그래밍 강의를 시작하며 – 초보도 끝까지 완주할 수 있는 실전 강의](/uploads/editor/PHP-%EA%B0%95%EC%9D%98-PHP-8-4-%EC%9B%B9-%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%B0%8D-%EA%B0%95%EC%9D%98%EB%A5%BC-%EC%8B%9C%EC%9E%91%ED%95%98%EB%A9%B0-%EC%B4%88%EB%B3%B4%EB%8F%84-%EB%81%9D%EA%B9%8C%EC%A7%80-%EC%99%84%EC%A3%BC%ED%95%A0-%EC%88%98-%EC%9E%88%EB%8A%94-%EC%8B%A4%EC%A0%84-%EA%B0%95%EC%9D%98-20260702-5787b7.webp)
Why Build a Forum?
When learning web programming, the first project many beginners create is a forum.
A forum contains all the essential functions of web development.
For example:
Data input
Data storage
List display
Detailed view
Editing
Deletion
You can experience all core web development functions through a forum project.
If you can build a forum, you can implement similar systems like user management, e-commerce sites, or admin panels using the same approach.
What is CRUD?
CRUD refers to the fundamental operations of database applications.
Function | Description |
|---|---|
Create | Data storage |
Read | Data retrieval |
Update | Data modification |
Delete | Data deletion |
This lesson will implement all four of these functions.
Preparing the Forum Table
First, create a forum table in your database.
CREATE TABLE board (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
content TEXT NOT NULL,
writer VARCHAR(50) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
This basic structure is sufficient for implementing forum functionality.
Project Structure
The file structure we'll use in this lesson:
board/
│
├── db.php
├── list.php
├── write.php
├── write_ok.php
├── view.php
├── edit.php
├── edit_ok.php
├── delete.php
└── delete_ok.php
Separating files by functionality makes maintenance much easier.
Connecting to the Database
Create a common connection file used across all pages.
<?php
$pdo = new PDO(
"mysql:host=localhost;dbname=php84;charset=utf8mb4",
"root",
""
);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
From now on, you'll only need to include this file using require_once.
Creating the Post Form
This is the page where users input their forum posts.
<form action="write_ok.php" method="post">
Title
<input type="text" name="title">
Author
<input type="text" name="writer">
Content
<textarea name="content"></textarea>
<button type="submit">
Submit
</button>
</form>
Storing Posts (Create)
Saves form data into the database.
<?php
require_once "db.php";
$sql = "
INSERT INTO board
(title, writer, content)
VALUES
(?, ?, ?)
";
$stmt = $pdo->prepare($sql);
$stmt->execute([
$_POST["title"],
$_POST["writer"],
$_POST["content"]
]);
header("Location:list.php");
exit;
After posting, the user is redirected to the list page.
Displaying the Post List (Read)
Displays stored forum posts.
<?php
require_once "db.php";
$stmt = $pdo->query("SELECT * FROM board ORDER BY id DESC");
$rows = $stmt->fetchAll();
List Display
<?php
foreach ($rows as $row) {
echo "<p>";
echo $row["title"];
echo "</p>";
}
Newest posts appear at the top.
Viewing Post Details
Clicking a title in the list shows the full content.
<?php
$stmt = $pdo->prepare("SELECT * FROM board WHERE id = ?");
$stmt->execute([$_GET["id"]]);
$post = $stmt->fetch();
Now display the title and content on screen.
Editing Posts (Update)
First, retrieve existing data.
<?php
$stmt = $pdo->prepare("SELECT * FROM board WHERE id=?");
$stmt->execute([$_GET["id"]]);
$post = $stmt->fetch();
In the edit form, display existing content in input fields and textareas.
Here's how to save updates:
<?php
$stmt = $pdo->prepare("UPDATE board SET title=?, content=? WHERE id=?");
$stmt->execute([
$_POST["title"],
$_POST["content"],
$_POST["id"]
]);
Deleting Posts (Delete)
Delete functionality is straightforward.
<?php
$stmt = $pdo->prepare("DELETE FROM board WHERE id=?");
$stmt->execute([$_GET["id"]]);
After deletion, redirect to the list page.
Forum Workflow
A forum operates in this sequence:
List
↓
Write Post
↓
Save
↓
List
↓
View Details
↓
Edit or Delete
Understanding this flow helps you build most web applications with ease.
Why Prevent SQL Injection?
Directly connecting user input to SQL queries can cause security issues.
Bad Example
$sql = "SELECT * FROM board WHERE id=" . $_GET["id"];
Good Example
$stmt = $pdo->prepare("SELECT * FROM board WHERE id=?");
$stmt->execute([$_GET["id"]]);
Using Prepared Statements prevents SQL injection effectively.
Common Features in Real Forums
In real-world applications, these features are often added alongside CRUD:
Pagination (page numbers)
Search functionality
View count increment
Display of post dates
Attachment files
Featured images
Comment system
This lesson focuses on implementing the fundamental CRUD operations first.
Common Beginner Mistakes
When building a forum for the first time, beginners often make these mistakes:
Failing to redirect to the list after INSERT
Not passing ID values correctly
Not retrieving existing data before editing
Not using Prepared Statements
Using only GET for DELETE without confirmation
Focusing on these points can prevent most common errors.
What You'll Complete in This Lesson
By the end of this lesson, you'll have implemented all these features:
Post creation
Post list display
Detailed post view
Post editing
Post deletion
You'll now have a fully functional basic forum.
In Conclusion
This lesson covered implementing the core CRUD functions (Create, Read, Update, Delete) for forums.
By combining what you've learned about variables, conditionals, functions, form handling, and database connections, you've experienced how a real web application is built.
In the next lesson, we'll add user login functionality and implement permissions so only registered users can write or edit posts, and administrators can delete posts - creating a more complete forum system.
FAQ
What is the purpose of a forum in web development?
A forum serves as a foundational project that demonstrates all essential web development functions, including data input, storage, display, and management.
Why are Prepared Statements important for security?
Prepared Statements prevent SQL injection attacks by separating user input from database queries.
Korean Source
This article is an English localization of a Korean-language report from BlogScore. Original: [PHP 강좌] 10강 게시판 글쓰기, 목록, 수정, 삭제(CRUD) 만들기