当前位置:首页 > PHP

php评论功能实现代码

2026-02-13 22:16:59PHP

基础PHP评论功能实现

数据库表结构设计(MySQL)

CREATE TABLE comments (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) NOT NULL,
    email VARCHAR(100),
    content TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    post_id INT NOT NULL
);

评论提交表单

<form action="submit_comment.php" method="POST">
    <input type="text" name="username" placeholder="Your Name" required>
    <input type="email" name="email" placeholder="Your Email">
    <textarea name="content" placeholder="Your Comment" required></textarea>
    <input type="hidden" name="post_id" value="<?php echo $post_id; ?>">
    <button type="submit">Submit Comment</button>
</form>

评论处理脚本(submit_comment.php)

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = htmlspecialchars($_POST['username']);
    $email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
    $content = htmlspecialchars($_POST['content']);
    $post_id = (int)$_POST['post_id'];

    if (!empty($username) && !empty($content)) {
        $db = new PDO('mysql:host=localhost;dbname=your_database', 'username', 'password');
        $stmt = $db->prepare("INSERT INTO comments (username, email, content, post_id) VALUES (?, ?, ?, ?)");
        $stmt->execute([$username, $email, $content, $post_id]);

        header("Location: post.php?id=$post_id");
        exit();
    }
}
?>

显示评论功能

<?php
$post_id = (int)$_GET['id'];
$db = new PDO('mysql:host=localhost;dbname=your_database', 'username', 'password');
$stmt = $db->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 '<h4>' . htmlspecialchars($comment['username']) . '</h4>';
    echo '<p>' . nl2br(htmlspecialchars($comment['content'])) . '</p>';
    echo '<small>' . $comment['created_at'] . '</small>';
    echo '</div>';
}
?>

安全增强措施

输入验证和过滤

php评论功能实现代码

$username = trim(strip_tags($_POST['username']));
$content = trim(strip_tags($_POST['content'], '<a><strong><em><code>'));

防止SQL注入

$stmt = $db->prepare("INSERT INTO comments (...) VALUES (?, ?, ?, ?)");
$stmt->bindParam(1, $username, PDO::PARAM_STR);
$stmt->bindParam(2, $email, PDO::PARAM_STR);
$stmt->bindParam(3, $content, PDO::PARAM_STR);
$stmt->bindParam(4, $post_id, PDO::PARAM_INT);

分页功能实现

获取分页参数

php评论功能实现代码

$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$per_page = 10;
$offset = ($page - 1) * $per_page;

修改查询语句

$stmt = $db->prepare("SELECT * FROM comments WHERE post_id = ? ORDER BY created_at DESC LIMIT ? OFFSET ?");
$stmt->bindValue(1, $post_id, PDO::PARAM_INT);
$stmt->bindValue(2, $per_page, PDO::PARAM_INT);
$stmt->bindValue(3, $offset, PDO::PARAM_INT);

生成分页链接

$total = $db->query("SELECT COUNT(*) FROM comments WHERE post_id = $post_id")->fetchColumn();
$pages = ceil($total / $per_page);

for ($i = 1; $i <= $pages; $i++) {
    echo "<a href='post.php?id=$post_id&page=$i'>$i</a> ";
}

标签: 代码功能
分享给朋友:

相关文章

vue怎么实现文本代码实现

vue怎么实现文本代码实现

Vue 实现文本代码展示的方法 在 Vue 中展示代码块或格式化文本,可以通过多种方式实现。以下是几种常见方法: 使用 <pre> 和 <code> 标签 最简单的原生 HT…

vue怎么实现文本代码实现

vue怎么实现文本代码实现

Vue 实现文本代码展示的方法 在 Vue 中展示代码文本通常需要结合语法高亮和格式化的功能,以下是几种常见的实现方式: 使用 <pre> 和 <code> 标签 通过 HT…

vue功能实现难点

vue功能实现难点

Vue 功能实现的常见难点 响应式系统深度监听 Vue 的响应式系统基于 Object.defineProperty(Vue 2)或 Proxy(Vue 3),但对嵌套对象或数组的深层属性变化需要额外…

vue实现换页功能

vue实现换页功能

Vue 实现分页功能 使用第三方库(如 Element UI) 安装 Element UI: npm install element-ui 在 main.js 中引入: import Vue fr…

vue图片实现功能

vue图片实现功能

图片上传功能实现 使用 <input type="file"> 结合 Vue 的 v-on:change 事件监听文件选择 <template> <input ty…

vue实现分类功能

vue实现分类功能

实现分类功能的基本思路 在Vue中实现分类功能通常涉及数据分组、动态渲染和交互逻辑。核心步骤包括准备分类数据、设计组件结构、实现分类切换逻辑。 数据准备与分组 分类功能的基础是将原始数据按特定规则分…