Building a Simple PHP Message Board with MySQL

This tutorial guides you through creating a simple PHP message board, covering MySQL database setup, table design, and step‑by‑step PHP scripts for displaying a form, handling submissions, and listing comments, while also noting security and extension considerations.

php Courses
php Courses
php Courses
Building a Simple PHP Message Board with MySQL

留言板是一个常见的Web应用程序,用于保存和展示用户提交的留言或评论。本文介绍如何使用PHP和MySQL实现一个简单的留言板。

首先创建名为 messages 的数据库,并在其中创建 comments 表,包含 id、name、email、message、created_at 等字段。

CREATE DATABASE messages;

USE messages;

CREATE TABLE comments (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50) NOT NULL,
    email VARCHAR(50) NOT NULL,
    message TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

表结构说明:

id:自增主键,用于唯一标识每条留言

name:留言者姓名,最多50字符

email:留言者邮箱,最多50字符

message:留言内容,使用 TEXT 存储

created_at:创建时间,使用时间戳记录

接下来创建 index.php ,其中包含一个HTML表单用于提交姓名、邮箱和留言,并在页面底部预留显示留言列表的区域。

<!DOCTYPE html>
<html>
<head>
    <title>留言板</title>
</head>
<body>
    <h1>留言板</h1>
    <form method="POST" action="save_comment.php">
        <label for="name">姓名:</label>
        <input type="text" id="name" name="name" required><br>
        <label for="email">邮箱:</label>
        <input type="email" id="email" name="email" required><br>
        <label for="message">留言:</label><br>
        <textarea id="message" name="message" required></textarea><br>
        <input type="submit" value="提交留言">
    </form>

    <h2>留言列表</h2>
    <?php
    // TODO: 显示留言列表
    ?>
</body>
</html>

然后编写 save_comment.php ,负责连接数据库、接收表单数据并使用 INSERT 语句保存到 comments 表,成功或失败后输出相应提示。

<?php
// 连接数据库
$servername = "localhost";
$username = "root";
$password = "your_password";
$dbname = "messages";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("数据库连接失败: " . $conn->connect_error);
}

// 处理表单提交
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST["name"];
    $email = $_POST["email"];
    $message = $_POST["message"];

    // 保存留言到数据库
    $sql = "INSERT INTO comments (name, email, message) VALUES ('$name', '$email', '$message')";

    if ($conn->query($sql) === TRUE) {
        echo "留言提交成功。";
    } else {
        echo "留言提交失败: " . $conn->error;
    }
}

// 关闭连接
$conn->close();
?>

最后在 index.php 中的 TODO 部分加入查询并显示留言列表的代码:

// 查询留言列表
$sql = "SELECT * FROM comments ORDER BY created_at DESC";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        echo "<p><strong>{$row['name']}</strong> [{$row['email']}]<br>{$row['message']}</p>";
        echo "<small>{$row['created_at']}</small>";
    }
} else {
    echo "暂无留言。";
}

至此,一个基本的留言板已完成,用户可以提交留言并查看列表,后续可加入分页、回复、输入验证等功能以提升安全性和可用性。

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

mysqlPHPMessage Board
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.