Berikut adalah tutorial lengkap membuat halaman forum sederhana menggunakan PHP. Forum ini memungkinkan pengguna untuk:
Melihat daftar topik
Membuat topik baru
Membalas topik
Anda bisa mengembangkannya kembali misal sebelum posting harus login terlebih dahulu.
🧱 Struktur Folder
/forum │ ├── config.php ├── index.php // Halaman utama: daftar topik ├── new_topic.php // Form tambah topik ├── topic.php // Tampilkan isi topik dan komentar ├── reply.php // Form balas topik └── forum.sql // Struktur database
🗃️ 1. Database MySQL (forum.sql)
CREATE DATABASE forum_db;
USE forum_db;
CREATE TABLE topics (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
content TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE replies (
id INT AUTO_INCREMENT PRIMARY KEY,
topic_id INT,
content TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (topic_id) REFERENCES topics(id) ON DELETE CASCADE
);
⚙️ 2. Koneksi Database (config.php)
<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'forum_db';
$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
die("Koneksi gagal: " . $conn->connect_error);
}
?>
🏠 3. Halaman Utama (index.php)
<?php include 'config.php'; ?>
<!DOCTYPE html>
<html>
<head><title>Forum Sederhana</title></head>
<body>
<h1>Forum Diskusi</h1>
<a href="new_topic.php">+ Buat Topik Baru</a>
<hr>
<?php
$result = $conn->query("SELECT * FROM topics ORDER BY created_at DESC");
while ($row = $result->fetch_assoc()):
?>
<h3><a href="topic.php?id=<?= $row['id'] ?>"><?= htmlspecialchars($row['title']) ?></a></h3>
<p><?= nl2br(htmlspecialchars($row['content'])) ?></p>
<small>Posted on <?= $row['created_at'] ?></small>
<hr>
<?php endwhile; ?>
</body>
</html>
✍️ 4. Buat Topik Baru (new_topic.php)
<?php include 'config.php'; ?>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$title = $conn->real_escape_string($_POST['title']);
$content = $conn->real_escape_string($_POST['content']);
$conn->query("INSERT INTO topics (title, content) VALUES ('$title', '$content')");
header("Location: index.php");
}
?>
<!DOCTYPE html>
<html>
<head><title>Topik Baru</title></head>
<body>
<h1>Buat Topik Baru</h1>
<form method="post">
Judul: <br><input type="text" name="title" required><br><br>
Isi: <br><textarea name="content" rows="6" cols="50" required></textarea><br><br>
<button type="submit">Kirim</button>
</form>
<a href="index.php">Kembali</a>
</body>
</html>
💬 5. Lihat Topik dan Balasan (topic.php)
<?php
include 'config.php';
$id = (int)$_GET['id'];
$topic = $conn->query("SELECT * FROM topics WHERE id=$id")->fetch_assoc();
$replies = $conn->query("SELECT * FROM replies WHERE topic_id=$id ORDER BY created_at ASC");
?>
<!DOCTYPE html>
<html>
<head><title><?= htmlspecialchars($topic['title']) ?></title></head>
<body>
<h1><?= htmlspecialchars($topic['title']) ?></h1>
<p><?= nl2br(htmlspecialchars($topic['content'])) ?></p>
<small>Dibuat: <?= $topic['created_at'] ?></small>
<hr>
<h3>Balasan:</h3>
<?php while ($row = $replies->fetch_assoc()): ?>
<div style="margin-left: 20px;">
<p><?= nl2br(htmlspecialchars($row['content'])) ?></p>
<small>Ditulis: <?= $row['created_at'] ?></small>
<hr>
</div>
<?php endwhile; ?>
<a href="reply.php?topic_id=<?= $id ?>">Balas Topik</a> | <a href="index.php">Kembali</a>
</body>
</html>
📨 6. Balas Topik (reply.php)
<?php include 'config.php'; ?>
<?php
$topic_id = (int)$_GET['topic_id'];
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$content = $conn->real_escape_string($_POST['content']);
$conn->query("INSERT INTO replies (topic_id, content) VALUES ($topic_id, '$content')");
header("Location: topic.php?id=$topic_id");
}
?>
<!DOCTYPE html>
<html>
<head><title>Balas Topik</title></head>
<body>
<h1>Balas Topik</h1>
<form method="post">
<textarea name="content" rows="6" cols="50" required></textarea><br><br>
<button type="submit">Kirim Balasan</button>
</form>
<a href="topic.php?id=<?= $topic_id ?>">Kembali</a>
</body>
</html>
✅ Penutup
Forum ini belum memiliki fitur login, keamanan CSRF, validasi lanjutan, atau pagination. Tapi sangat cocok untuk:
Belajar dasar PHP dan MySQL
Membuat prototype sistem diskusi.










