게시판(글 작성 & 조회) 구현하기
1. 게시판 테이블 만들기
CREATE TABLE board (
id INT AUTO_INCREMENT PRIMARY KEY,
notice_type VARCHAR(50) NOT NULL,
user_id VARCHAR(50) NOT NULL,
title VARCHAR(255) NOT NULL,
content TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
2. BoardClass.php
<?php
class BoardClass {
private $conn;
private $table_name = "board";
public function __construct($conn) {
$this->conn = $conn;
}
public function create($notice_type, $user_id, $title, $content) {
$sql = "INSERT INTO {$this->table_name} (notice_type, user_id, title, content, created_at)
VALUES (?, ?, ?, ?, NOW())";
$stmt = $this->conn->prepare($sql);
if (!$stmt) {
die("SQL prepare 실패: " . $this->conn->error);
}
$stmt->bind_param("siss", $notice_type, $user_id, $title, $content);
return $stmt->execute();
}
public function getList() {
$sql = "SELECT
b.id,
b.notice_type,
b.user_id,
u.name AS user_name,
b.title,
b.content,
b.created_at
FROM {$this->table_name} b
LEFT JOIN users u ON b.user_id = u.id
ORDER BY b.created_at DESC";
$result = $this->conn->query($sql);
$data = [];
if ($result && $result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$data[] = $row;
}
}
return $data;
}
public function getView($id) {
$sql = "SELECT b.*, u.name AS user_name
FROM {$this->table_name} b
LEFT JOIN user u ON b.user_id = u.id
WHERE b.id = ?";
$stmt = $this->conn->prepare($sql);
if (!$stmt) {
die("SQL prepare 실패: " . $this->conn->error);
}
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
return $result->fetch_assoc();
}
public function delete($id, $user_id) {
$sql = "DELETE FROM {$this->table_name} WHERE id = ? AND user_id = ?";
$stmt = $this->conn->prepare($sql);
if (!$stmt) {
die("SQL prepare 실패: " . $this->conn->error);
}
$stmt->bind_param("ii", $id, $user_id);
return $stmt->execute();
}
}
?>
3. 글쓰기 폼
📘 post_write.php
<?php
session_start();
if (!isset($_SESSION['is_login']) || $_SESSION['is_login'] !== true) {
echo "<script>alert('로그인 후 이용할 수 있습니다.'); location.href='signin.php';</script>";
exit;
}
?>
<h2>게시글 작성</h2>
<form action="procs/post_write_proc.php" method="post">
<input type="text" name="title" placeholder="Title" required><br>
<textarea name="content" rows="6" placeholder="Content" required></textarea><br>
<button type="submit">POST</button>
</form>

4. 글쓰기 처리
📘 post_write_proc.php
<?php
session_start();
include '../db.php';
include '../classes/BoardClass.php';
if (!isset($_SESSION['user_id'])) {
echo "<script>alert('로그인 후 이용해주세요.'); location.href='../signin.php';</script>";
exit;
}
$user_id = $_SESSION['user_id'];
$title = trim($_POST['title']);
$content = trim($_POST['content']);
$notice_type = 'post';
$board = new BoardClass($conn);
if ($board->create($notice_type, $user_id, $title, $content)) {
echo "<script>alert('게시글이 등록되었습니다.'); location.href='../index.php';</script>";
} else {
echo "<script>alert('등록 중 오류가 발생했습니다.'); history.back();</script>";
}
?>
5. 글 목록
📘 post_list.php
<div class="row">
<div class="col-md-12">
<?php if (!empty($posts)): ?>
<?php foreach ($posts as $post): ?>
<div class="col-md-12">
<div class="fh5co-blog animate-box">
<div class="blog-text">
<span class="posted_on">
<?= date('Y.m.d', strtotime($post['created_at'])) ?>
</span>
<h3>
<a href="post_view.php?id=<?= $post['id'] ?>">
<?= htmlspecialchars($post['title']) ?>
</a>
</h3>
<p><?= htmlspecialchars(mb_strimwidth($post['content'], 0, 100, '...')) ?></p>
<ul class="stuff">
<li><i class="icon-user"></i> <?= htmlspecialchars($post['user_name']) ?></li>
<li><i class="icon-eye2"></i> 조회수 0</li>
<li>
<a href="post_view.php?id=<?= $post['id'] ?>">
Read More<i class="icon-arrow-right22"></i>
</a>
</li>
</ul>
</div>
</div>
</div>
<?php endforeach; ?>
<?php else: ?>
<p class="text-center">등록된 게시글이 없습니다.</p>
<?php endif; ?>
</div>
</div>

6. 글 상세보기
📘 post_view.php
<?php
session_start();
include '../db.php';
include '../classes/BoardClass.php';
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
echo "<script>alert('잘못된 접근입니다.'); history.back();</script>";
exit;
}
$id = (int)$_GET['id'];
$board = new BoardClass($conn);
$post = $board->getView($id);
if (!$post) {
echo "<script>alert('존재하지 않는 게시글입니다.'); history.back();</script>";
exit;
}
?>
<h2>게시글 상세보기</h2>
<p><strong>제목:</strong> <?= htmlspecialchars($post['title']) ?></p>
<p><strong>작성자:</strong> <?= htmlspecialchars($post['user_name']) ?></p>
<p><strong>작성일:</strong> <?= htmlspecialchars($post['created_at']) ?></p>
<hr>
<p><?= nl2br(htmlspecialchars($post['content'])) ?></p>
<hr>
<a href="post_list.php">목록으로</a>
<?php if (isset($_SESSION['user_id']) && $_SESSION['user_id'] == $post['user_id']): ?>
<form action="procs/post_delete_proc.php" method="post" style="display:inline;">
<input type="hidden" name="id" value="<?= $post['id'] ?>">
<button type="submit" onclick="return confirm('정말 삭제하시겠습니까?')">삭제</button>
</form>
<?php endif; ?>
7. 게시글 삭제 처리
📘 post_delete_proc.php
<?php
session_start();
include '../db.php';
include '../classes/BoardClass.php';
if (!isset($_SESSION['user_id'])) {
echo "<script>alert('로그인 후 이용해주세요.'); location.href='../signin.php';</script>";
exit;
}
$user_id = $_SESSION['user_id'];
$id = (int)$_POST['id'];
$board = new BoardClass($conn);
if ($board->delete($id, $user_id)) {
echo "<script>alert('게시글이 삭제되었습니다.'); location.href='../post_list.php';</script>";
} else {
echo "<script>alert('삭제 중 오류가 발생했습니다.'); history.back();</script>";
}
?>