web建站教程
  1. 首页
  2. vuejs
  3. js
  4. 好玩
  5. seo教程
  6. 前端知识
  7. 百度echarts
  8. 更多
    php入门
    nodejs
    mockjs
    reactjs
    mysql
    wordpress
    织梦cms
    帝国cms
    git教程
    IT知识
    模板大全
    休息站

如何利用php语法实现留言板的回复功能(前后端结合)

626 ℃
           

前端代码:

<!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首页进行判断(根据需要显示不同页面)

html文本如何读取数据库代码示例

php表单提交报错显示适当的错误消息代码

php语法中sleep函数的基本用法

利用php语法中NOT IN排除多个字段功能

标签: php语法, 留言板回复功能

上面是“如何利用php语法实现留言板的回复功能(前后端结合)”的全面内容,想了解更多关于 php入门 内容,请继续关注web建站教程。

当前网址:https://ipkd.cn/webs_4161.html

声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

当前位置: 网站首页 > php入门
本文共计4305个字,预计阅读时长29分钟
生活小工具,收录了80多款小工具
上一篇: 推荐一款简单高效漂亮的多平台笔记应用——Zoho Notebook
下一篇: 推荐2款充满科技感的艺术字体——小新黑体、小新潮酷体
x 打工人ai神器