欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

PHP 完整代码示例:留言板

最编程 2024-03-20 07:47:32
...

本篇文章给大家分享怎么用PHP来实现一个留言板的功能。

一、留言板需要实现的功能

1:写留言、展示留言

其中包括留言内容、留言人、留言时间;下面的实力中还包括了指定的qq账号头像;将上述中的留言内容、留言人以及时间等数据展示出来

2:编辑、更新、删除留言

编辑指定的留言内容,并更新显示,或者删除

3:回复留言,展示、编辑、删除回复

二、设计表

1:留言表message

我们需要根据想要展示的数据来创建字段,例如id、姓名、留言内容、时间、qq账号等等

示例建表语句如下:

CREATE TABLE `message` (
  `mid` int(11) NOT NULL,
  `name` varchar(20) NOT NULL,
  `text` varchar(200) NOT NULL,
  `time` datetime NOT NULL,
  `qqnum` varchar(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

2:回复表reply

重新创建一个回复表,其中一定要有的字段是message中留言id,回复中的内容要与指定的留言对应

示例建表语句如下:

CREATE TABLE `reply` (
  `reid` int(11) NOT NULL,
  `mid` int(11) NOT NULL,
  `replyname` varchar(20) NOT NULL,
  `replytext` varchar(200) NOT NULL,
  `replytime` datetime NOT NULL,
  `replyqqnum` varchar(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

三:留言代码详情

完整代码下载:https://gitee.com/wang-boy326/message.git

1:database.php

用于连接到 MySQL 数据库,以便后续的数据库操作,例如查询、插入、更新或删除数据。如果连接成功,你可以使用 $conn 对象执行各种数据库操作。

<?php
// 指定 MySQL 数据库服务器的主机名或 IP 地址。
$servername = "localhost";
// 指定连接数据库所需的用户名。
$username = "root";
// 指定连接数据库所需的密码。
$password = "root";
// 指定要连接的数据库的名称。
$dbname = "message";

//创建链接
$conn = new mysqli($servername,$username,$password,$dbname);
//监测链接
if($conn->connect_error){
    die("链接失败:". $conn->connect_error);
}

2:config.php

利用require_once引入database里的代码,并定义了一个全局变量 $title,用于存储全站的标题。

<?php
// 链接数据库
require_once 'database.php';
//全站标题
$title = "我的留言板";

3:index.php

首页,创建一个基本的留言板页面,包括留言输入表单和已有留言的展示功能

循环遍历每个留言记录,并将其内容以适当的格式显示出来,包括留言楼层、留言人的 QQ 头像、留言内容、留言人姓名、留言时间等。

对于每个留言记录,还提供了三个链接,分别用于查看回复、编辑和删除该留言。

<?php
require_once 'config.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 引入css文件 -->
    <link rel="stylesheet" type="text/css" href="./style/index.css">
<!-- 全站标题 -->
    <title><?php echo $title; ?></title>
</head>
<body>
    <h1><?php echo $title; ?></h1>
    <!-- 写留言 -->
    <form action="add.php" method="get">
        <textarea name="text" id="" cols="30" rows="10" placeholder="在此处留言..."></textarea>
        <p><input type="text" name="name" placeholder="姓名"></p>
        <p><input type="text" name="qqnum" placeholder="qq号"></p>
        <p><input type="submit" value="发表"></p>
    </form>
    <hr>
    <!-- 展示留言 -->
    <ul>
        <?php
        //最新留言展示在最前面
        $sql = "SELECT * FROM `message` ORDER BY `message`.`mid` DESC";
        //执行sql语句
        $result = $conn->query($sql);
        if($result->num_rows > 0){
            //输出数据
            while($row = $result->fetch_assoc()){
                ?>
                <li>
                <p><?php echo $row["mid"];?>楼</p>
                <!-- 展示qq头像 -->
                <p><img src="https://q1.qlogo.cn/g?b=qq&nk=<?php echo $row["qqnum"];?>&s=640" alt="" width="100"></p>
                <p>留言内容:<?php echo $row["text"];?></p>
                <p>留言人:<?php echo $row["name"];?></p>
                <p>留言时间:<?php echo $row["time"];?></p>
                <p>
                    <a href="reply.php?id=<?php echo $row["mid"];?>">查看回复</a>
                    <a href="edit.php?id=<?php echo $row["mid"];?>">编辑</a>
                    <a href="del.php?id=<?php echo $row["mid"];?>">删除</a>
                </p>
                </li>
                <?php
            }
        }else{
            //没有留言
            echo "暂无留言";
        }
        ?>
    </ul>

</body>
</html>

4:add.php

当在首页点击发表的时候,<form action="add.php" method="get">,跳转到add.php

那么add的作用就是将用户提交的留言信息插入到数据库中,并在插入完成后将页面重定向到留言板的首页。

<?php
require_once 'config.php';
// 获取名为 text 的参数值,并将其赋给变量 $text
$text = $_GET["text"];
// 获取名为 name 的参数值,并将其赋给变量 $name
$name = $_GET["name"];
// 获取当前时间
$time = date("Y-m-d H:i:s",time());
// 获取名为 qqnum 的参数值,并将其赋给变量 $qqnum
$qqnum = $_GET["qqnum"];
//插入语句
$sql = "INSERT INTO `message` (`mid`,`name`,`text`,`time`,`qqnum`) VALUES (NULL,'$name','$text','$time','$qqnum');";
// 执行了插入语句,将留言信息插入到数据库中。
$conn->query($sql);
//跳转到首页
header("Location:index.php");

5:edit.php

当首页点击编辑“编辑”留言的时候,会跳转到add.php。

<a href="edit.php?id=<?php echo $row["mid"];?>">编辑</a>

这段代码的作用是创建一个用于编辑留言的页面,其中包含了一个表单,用于更新留言内容,并提供了返回留言详情的功能。用户可以在该页面中编辑指定 ID 的留言内容,并提交表单进行更新操作。

<?php
require_once 'config.php';
// 获取了要编辑的留言的 ID,并将其赋值给变量 $id。
$id = $_GET["id"];
// 查询数据库中指定 ID 的留言记录。
$sql = "SELECT * FROM `message` WHERE `mid` = $id";
$result = $conn->query($sql);
if($result->num_rows > 0){
    // 如果有查询结果,将查询结果中的留言内容赋值给变量 $text。
    $res = $result->fetch_assoc();
    $text = $res["text"];
}else{
    // 如果没有查询结果,输出错误信息并终止脚本的执行。
    die("无此条留言");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>编辑</title>
</head>
<body>
    <h1><?php echo $title; ?></h1>
    <h2>编辑<?php echo $id;?>楼留言内容</h2>
    <form action="update.php" method="get">
        <input type="text" name="id" value="<?php echo $id; ?>" hidden>
        <textarea name="text" id="" cols="30" rows="10"><?php echo $text; ?></textarea>
        <p><input type="submit" value="更新留言"></p>
    </form>
    <form action="http://liuyan.cn/index.php" method="post">
        <button type="submit">返回留言详情</button>
    </form>
</body>
</html>

6:update.php

上述代码点击“更新留言”会传到update.php

总结起来,这段代码的作用是从 GET 请求中获取留言的 ID 和更新后的内容,并执行更新操作将留言内容更新到数据库中。更新完成后,将页面重定向到留言板的首页。

<?php
require_once 'config.php';
$id = $_GET["id"];
$text = $_GET["text"];
// 用于输出变量 $id 和 $text 的值,用于调试目的。
var_dump($id);
var_dump($text);
//编辑  更新语句
// 将指定 ID 的留言内容更新为变量 $text 的值。
$sql = "UPDATE `message` SET `text` = '$text' WHERE `message`.`mid` = $id;";
//执行更新
$conn->query($sql);
// 这行代码将页面重定向到名为 index.php 的页面,即留言板的首页。
header("Location:index.php");

7:del.php

在首页代码中点击“删除”会传到del.php

这段代码的作用是从 GET 请求中获取留言的 ID,并执行删除操作将指定 ID 的留言从数据库中删除。删除完成后,将页面重定向到留言板的首页。

<?php
require_once 'config.php';
$id = $_GET["id"];
var_dump($id);
//构建了一个删除语句,用于删除指定 ID 的留言。
$sql = "DELETE FROM `message` WHERE `message`.`mid` = $id;";
$conn->query($sql);
header("Location:index.php");

8:css代码

/* 设置全局样式 */
body {
    font-family: Arial, sans-serif;
    background-color: #f2f2f2;
    margin: 0;
    padding: 20px;
}
h1, h2 {
    color: #333;
}
/* 设置标题样式 */
h1 {
    font-size: 24px;
    text-align: center;
}
h2 {
    font-size: 40px;
    margin-top: 20px;
    text-align: center;
}
/* 设置表单样式 */
form {
    margin-bottom: 20px;
}
textarea, input[type="text"], input[type="submit"] {
    width: 100%;
    padding: 10px;
    margin-bottom: 10px;
    border: 1px solid #ccc;
    border-radius: 4px;
}
input[type="submit"] {
    background-color: #4CAF50;
    color: white;
    cursor: pointer;
}
/* 设置留言列表样式 */
ul {
    list-style-type: none;
    padding: 0;
}
li {
    background-color: #fff;
    padding: 10px;
    margin-bottom: 10px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
li p {
    margin: 0;
}
li img {
    display: block;
    margin: 10px auto;
    max-width: 100%;
    height: auto;
}
li a {
    color: #333;
    text-decoration: none;
    margin-right: 10px;
}
/* 响应式布局 */
@media (min-width: 768px) {
    form, ul {
        max-width: 600px;
        margin: 0 auto;
    }
}

至此就算基本完成了写留言、展示、编辑、更新、删除留言的操作了

下面来看一下,回复的代码详情,因为思路其实基本一样,就不过多赘述了

四:回复代码详情

1:reply.php

总结起来,这段代码的作用是创建一个用于回复留言的页面,其中包含了一个表单,用于回复留言并提交回复内容,以及显示已有的回复内容。用户可以在该页面中回复指定 ID 的留言,并提交表单进行回复操作。同时,页面还显示了留言的详细内容和已有的回复内容。

<?php
require_once 'config.php';
//接收id参数
$id = $_GET["id"];
//查询这条留言的内容
$sql = "SELECT * FROM `message` WHERE `mid` = $id";
$result = $conn->query($sql);
if($result->num_rows > 0){
    //输出单条留言内容
    $res = $result->fetch_assoc();
    $text = $res["text"];
    $name = $res["name"];
    $time = date("Y-m-d H:i:s",time());
    $qqnum = $res["qqnum"];
}else{
    die("无此条留言");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>回复</title>
</head>
<body>
    <h1><?php echo $title; ?></h1>
    <h2><?php echo $id;?>楼留言内容</h2>
    <p><img src="https://q1.qlogo.cn/g?b=qq&nk=<?php echo $qqnum;?>&s=640" alt="" width="100"></p>
    <p>留言内容:<?php echo $text;?></p>
    <p>留言人:<?php echo $name;?></p>
    <p>留言时间:<?php echo $time;?></p>
    <hr>
    <h2>我的回复</h2>
    <form action="replyadd.php" method="get">
        <input type="text" name="mid" value="<?php echo $id; ?>" hidden>
        <textarea name="retext" id="" cols="30" rows="10" placeholder="在此处回复..."></textarea>
        <p><input type="text" name="rename" placeholder="姓名"></p>
        <p><input type="text" name="reqqnum" placeholder="qq号"></p>
        <p><input type="submit" value="回复留言"></p>
    </form>
    <form action="index.php" method="post">
        <button type="submit">返回留言板</button>
    </form>
    <hr>
    <h2>评论</h2>
    <ul>
        <?php
        //最新留言展示在最前面
        $resql = "SELECT * FROM `reply` WHERE `mid` = $id ORDER BY `reply`.`mid` DESC";
        //执行sql语句
        $reresult = $conn->query($resql);
        if($reresult->num_rows > 0){
            //输出数据
            while($row = $reresult->fetch_assoc()){
                ?>
                <li>
                <p>回复<?php echo $row["reid"];?>楼</p>
                <p><img src="https://q1.qlogo.cn/g?b=qq&nk=<?php echo $row["replyqqnum"];?>&s=640" alt="" width="100"></p>
                <p>回复内容:<?php echo $row["replytext"];?></p>
                <p>回复人:<?php echo $row["replyname"];?></p>
                <p>回复时间:<?php echo $row["replytime"];?></p>
                <p>
                    <a href="reedit.php?id=<?php echo $row["reid"];?>">编辑</a>
                    <a href="redel.php?id=<?php echo $row["reid"];?>">删除</a>
                   
                </p>
                </li>
                <?php
            }
        }else{
            //没有留言
            echo "暂无回复";
        }
        ?>
    </ul>
</body>
</html>

2:replyadd.php

点击回复留言,将回复内容插入到数据库中,并跳转到回复页面

<?php
require_once 'config.php';
$mid = $_GET["mid"];
$retext = $_GET["retext"];
$rename = $_GET["rename"];
$retime = date("Y-m-d H:i:s",time());
$reqqnum = $_GET["reqqnum"];
//插入语句
$sql = "INSERT INTO `reply` (`reid`,`mid`,`replyname`,`replytext`,`replytime`,`replyqqnum`) VALUES (NULL,'$mid','$rename','$retext','$retime','$reqqnum');";
//执行语句
$conn->query($sql);
//跳转到回复页面
header("Location:http://liuyan.cn/reply.php?id="."$mid");

3:reedit.php

这段代码就和edit.php差不多了,就是更新的表不一样,内容不同

<?php
require_once 'config.php';
//接收id参数
$reid = $_GET["id"];
//查询这条留言的内容
$sql = "SELECT * FROM `reply` WHERE `reid` = $reid";
$result = $conn->query($sql);
if($result->num_rows > 0){
    //输出单条留言内容
    $res = $result->fetch_assoc();
    $retext = $res["replytext"];
    $mid = $res["mid"];
}else{
    die("无此条留言");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>编辑回复</title>
</head>
<body>
    <h1><?php echo $title; ?></h1>
    <h2>编辑<?php echo $reid;?>楼回复的内容</h2>
    <form action="reupdate.php" method="get">
        <input type="text" name="mid" value="<?php echo $mid; ?>" hidden>
        <input type="text" name="id" value="<?php echo $reid; ?>" hidden>
        <textarea name="text" id="" cols="30" rows="10"><?php echo $retext; ?></textarea>
        <p><input type="submit" value="更新回复"></p>
    </form>
    <form action="http://liuyan.cn/reply.php?id=<?php echo $mid; ?>" method="post">
        <button type="submit">返回留言详情</button>
    </form>
</body>
</html>

4:reupdate.php

点击更新回复,这段代码和update.php也是一样,只是更新的表不同

<?php
require_once 'config.php';
$mid = $_GET["mid"];
$id = $_GET["id"];
$text = $_GET["text"];
var_dump($id);
var_dump($text);
//编辑  更新语句
$sql = "UPDATE `reply` SET `replytext` = '$text' WHERE `reply`.`reid` = $id;";
//执行更新
$conn->query($sql);
header("Location:http://liuyan.cn/reply.php?id="."$mid");

5:redel.php

同样的这段代码和del.php也是一样,只是查询的表不同

<?php
require_once 'config.php';
$id = $_GET["id"];
var_dump($id);
//查询这条留言的内容
$sql = "SELECT * FROM `reply` WHERE `reid` = $id";
$result = $conn->query($sql);
if($result->num_rows > 0){
    //输出单条留言内容
    $res = $result->fetch_assoc();
    $mid = $res["mid"];
}else{
    die("无此条留言");
}
//删除语句
$delsql = "DELETE FROM `reply` WHERE `reply`.`reid` = $id;";
$conn->query($delsql);
// $resql = "SELECT * FROM `reply` WHERE `mid` = $id";
header("Location:http://liuyan.cn/reply.php?id="."$mid");

至此算是全部完成了回复留言,展示、编辑、删除回复的操作。

完整代码下载:https://gitee.com/wang-boy326/message.git