如何利用php语法实现留言板的回复功能(前后端结合)
前端代码:
<!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语法实现留言板的回复功能(前后端结合)”的全面内容,想了解更多关于 php入门 内容,请继续关注编程基础学习网。