KOSUNI KOREA
Korean News in English
Technology

[PHP Tutorial] Building a Forum with CRUD Operations (Create, Read, Update, Delete)

Published October 18, 2026 · 1 views
[PHP Tutorial] Building a Forum with CRUD Operations (Create, Read, Update, Delete)

[PHP 강의] PHP 8.4 웹 프로그래밍 강의를 시작하며 – 초보도 끝까지 완주할 수 있는 실전 강의


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) 만들기