当前位置:首页 > PHP

php实现评论功能

2026-01-15 12:59:05PHP

数据库设计

创建评论表(示例结构):

CREATE TABLE `comments` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `content` text NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `parent_id` int(11) DEFAULT NULL COMMENT '回复的评论ID',
  `post_id` int(11) NOT NULL COMMENT '关联的文章或内容ID',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

表单提交处理

创建评论提交表单(HTML部分):

<form action="submit_comment.php" method="POST">
  <input type="hidden" name="post_id" value="<?php echo $post_id; ?>">
  <textarea name="content" required></textarea>
  <button type="submit">提交评论</button>
</form>

PHP处理逻辑(submit_comment.php):

php实现评论功能

<?php
session_start();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  $content = htmlspecialchars($_POST['content']);
  $post_id = intval($_POST['post_id']);
  $user_id = $_SESSION['user_id'] ?? 0; // 假设用户已登录

  $pdo = new PDO('mysql:host=localhost;dbname=test', 'username', 'password');
  $stmt = $pdo->prepare("INSERT INTO comments (user_id, content, post_id) VALUES (?, ?, ?)");
  $stmt->execute([$user_id, $content, $post_id]);

  header("Location: post.php?id=".$post_id); // 返回原页面
}
?>

评论列表展示

从数据库查询并显示评论:

<?php
$post_id = intval($_GET['id']);
$pdo = new PDO('mysql:host=localhost;dbname=test', 'username', 'password');
$stmt = $pdo->prepare("SELECT * FROM comments WHERE post_id = ? ORDER BY created_at DESC");
$stmt->execute([$post_id]);
$comments = $stmt->fetchAll(PDO::FETCH_ASSOC);

foreach ($comments as $comment) {
  echo '<div class="comment">';
  echo '<p>'.htmlspecialchars($comment['content']).'</p>';
  echo '<small>用户ID: '.$comment['user_id'].' | 时间: '.$comment['created_at'].'</small>';
  echo '</div>';
}
?>

回复功能实现

扩展表单支持回复:

php实现评论功能

<form action="submit_comment.php" method="POST">
  <input type="hidden" name="post_id" value="<?php echo $post_id; ?>">
  <input type="hidden" name="parent_id" value="<?php echo $_GET['reply_to'] ?? 0; ?>">
  <textarea name="content" required></textarea>
  <button type="submit">提交</button>
</form>

更新数据库插入逻辑:

$parent_id = isset($_POST['parent_id']) ? intval($_POST['parent_id']) : null;
$stmt = $pdo->prepare("INSERT INTO comments (user_id, content, post_id, parent_id) VALUES (?, ?, ?, ?)");
$stmt->execute([$user_id, $content, $post_id, $parent_id]);

嵌套评论展示

递归显示评论及回复:

function displayComments($comments, $parent_id = null) {
  foreach ($comments as $comment) {
    if ($comment['parent_id'] == $parent_id) {
      echo '<div class="comment" style="margin-left: '.($parent_id ? '20px' : '0').'">';
      echo '<p>'.htmlspecialchars($comment['content']).'</p>';
      echo '<a href="?id='.$comment['post_id'].'&reply_to='.$comment['id'].'">回复</a>';
      displayComments($comments, $comment['id']); // 递归调用
      echo '</div>';
    }
  }
}
displayComments($comments);

安全注意事项

  1. 始终使用预处理语句防止SQL注入
  2. 输出内容时使用htmlspecialchars()过滤XSS攻击
  3. 验证用户权限,确保登录后才能评论
  4. 对高频提交实施限流措施

性能优化建议

  1. post_idparent_id字段添加索引
  2. 实现分页查询避免一次性加载大量评论
  3. 考虑使用缓存机制存储热门帖子的评论

完整实现需根据实际项目需求调整数据库结构和业务逻辑。

标签: 功能php
分享给朋友:

相关文章

php实现打印功能

php实现打印功能

PHP 实现打印功能的方法 PHP 可以通过多种方式实现打印功能,具体取决于打印的目标(如打印机、PDF、网页等)和需求(如直接打印、生成打印预览等)。以下是几种常见的实现方式: 使用 JavaSc…

h5能实现的功能

h5能实现的功能

H5能实现的功能 H5(HTML5)是一种现代的网页技术标准,具备丰富的功能和特性,适用于多种场景。以下是H5能够实现的主要功能: 多媒体支持 H5原生支持音频和视频播放,无需依赖Flash等插件。…

php实现队列

php实现队列

PHP实现队列的方法 队列是一种先进先出(FIFO)的数据结构,PHP可以通过多种方式实现队列功能。 使用数组模拟队列 PHP数组可以模拟队列的基本操作,通过array_push和array_shi…

uniapp支付功能怎么实现

uniapp支付功能怎么实现

uniapp支付功能实现方法 准备工作 注册微信支付、支付宝等平台的开发者账号,获取必要的商户ID(mch_id)、API密钥(key)、应用ID(appid)等信息。确保项目已配置好相关支付SDK。…

uniapp实现选项卡功能

uniapp实现选项卡功能

实现选项卡功能的基本方法 在uniapp中实现选项卡功能通常需要使用uni-segmented-control组件或自定义样式结合swiper组件。以下是两种常见实现方式: 使用uni-se…

vue实现功能插件

vue实现功能插件

Vue 功能插件实现方法 插件基本结构 Vue 插件通常需要暴露一个 install 方法,该方法接收 Vue 构造函数作为参数。基本结构如下: const MyPlugin = { insta…