前端代码:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>留言板</title> <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>留言板</h2> <form class="form-horizontal" method="post" action="post_message.php"> <div class="form-group"> <label for="username" class="col-sm-2 control-label">姓名:</label> <div class="col-sm-10"> <input type="text" name="username" class="form-control" required> </div> </div> <div class="form-group"> <label for="content" class="col-sm-2 control-label">留言内容:</label> <div class="col-sm-10"> <textarea name="content" class="form-control" rows="5" required></textarea> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-primary">提交</button> </div> </div> </form> <?php // 查询留言列表 $sql = "SELECT * FROM message ORDER BY created_at DESC"; $result = mysqli_query($conn, $sql); while ($row = mysqli_fetch_assoc($result)) { ?> <div class="panel panel-default"> <div class="panel-heading"> <?php echo $row['username']; ?>(<?php echo $row['created_at']; ?>) </div> <div class="panel-body"> <?php echo $row['content']; ?> </div> </div> <?php // 查询回复列表 $sql = "SELECT * FROM reply WHERE message_id = {$row['id']} ORDER BY created_at ASC"; $result2 = mysqli_query($conn, $sql); while ($row2 = mysqli_fetch_assoc($result2)) { ?> <div class="panel panel-info"> <div class="panel-heading"> <?php echo $row2['username']; ?>(<?php echo $row2['created_at']; ?>) </div> <div class="panel-body"> <?php echo $row2['content']; ?> </div> </div> <?php } ?> <form class="form-horizontal" method="post" action="post_reply.php"> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <input type="hidden" name="message_id" value="<?php echo $row['id']; ?>"> <input type="text" name="username" class="form-control" placeholder="回复者姓名" required> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <textarea name="content" class="form-control" rows="3" placeholder="回复内容" required></textarea> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default">回复</button> </div> </div> </form> <?php } ?> </div> </body> </html>
后端代码:
<?php // 建立数据库连接 $conn = new mysqli("localhost", "root", "password", "test"); if ($conn->connect_error) { die("数据库连接失败:" . $conn->connect_error); } // 定义Message类 class Message { public $id; public $username; public $content; public $created_at; public $updated_at; // 构造函数 function __construct($id, $username, $content, $created_at, $updated_at) { $this->id = $id; $this->username = $username; $this->content = $content; $this->created_at = $created_at; $this->updated_at = $updated_at; } // 静态方法:获取留言列表 static function get_list($conn) { $messages = array(); $sql = "SELECT * FROM message ORDER BY created_at DESC"; $result = mysqli_query($conn, $sql); while ($row = mysqli_fetch_assoc($result)) { $message = new Message($row['id'], $row['username'], $row['content'], $row['created_at'], $row['updated_at']); array_push($messages, $message); } return $messages; } } // 定义Reply类 class Reply { public $id; public $message_id; public $username; public $content; public $created_at; public $updated_at; // 构造函数 function __construct($id, $message_id, $username, $content, $created_at, $updated_at) { $this->id = $id; $this->message_id = $message_id; $this->username = $username; $this->content = $content; $this->created_at = $created_at; $this->updated_at = $updated_at; } // 静态方法:根据留言ID获取回复列表 static function get_list_by_message_id($conn, $message_id) { $replies = array(); $sql = "SELECT * FROM reply WHERE message_id = {$message_id} ORDER BY created_at ASC"; $result = mysqli_query($conn, $sql); while ($row = mysqli_fetch_assoc($result)) { $reply = new Reply($row['id'], $row['message_id'], $row['username'], $row['content'], $row['created_at'], $row['updated_at']); array_push($replies, $reply); } return $replies; } } // 处理留言提交 if ($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['username']) && !empty($_POST['content'])) { $sql = "INSERT INTO message (username, content) VALUES ('{$_POST['username']}', '{$_POST['content']}')"; mysqli_query($conn, $sql); } // 处理回复提交 if ($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['message_id']) && !empty($_POST['username']) && !empty($_POST['content'])) { $sql = "INSERT INTO reply (message_id, username, content) VALUES ({$_POST['message_id']}, '{$_POST['username']}', '{$_POST['content']}')"; mysqli_query($conn, $sql); } // 获取留言列表 $messages = Message::get_list($conn); ?>
php语法对index.php首页进行判断(根据需要显示不同页面)
上面是“如何利用php语法实现留言板的回复功能(前后端结合)”的全面内容,想了解更多关于 php入门 内容,请继续关注web建站教程。
当前网址:https://ipkd.cn/webs_4161.html
workflows工作流
- 去掉背景(抠图)comfyui工作流
- 葡萄庄园白葡萄酒ComfyUI工作流
- 一个漂亮的混血女孩ComfyUI工作流
- 汗血宝马ComfyUI工作流
- 冬天的严寒里红梅枝上停留着一只鸟
- 一个男孩在吃西瓜ComfyUI工作流
- 一杯咖啡蒸汽形成云ComfyUI工作流
- 一只蚂蚁在花丛中找食物ComfyUI工作流
猜你喜欢
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!