php实现和MySQL实现评论
PHP 实现评论功能
使用 PHP 和 MySQL 实现评论功能需要创建数据库表、编写 PHP 脚本处理表单提交,以及显示评论内容。
创建 MySQL 表存储评论数据:
CREATE TABLE comments (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100),
comment TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
创建评论表单 HTML:
<form action="post_comment.php" method="post">
<input type="text" name="name" placeholder="Your Name" required>
<input type="email" name="email" placeholder="Your Email">
<textarea name="comment" placeholder="Your Comment" required></textarea>
<button type="submit">Submit Comment</button>
</form>
处理表单提交的 PHP 脚本 (post_comment.php):
<?php
$conn = new mysqli('localhost', 'username', 'password', 'database');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$name = $conn->real_escape_string($_POST['name']);
$email = $conn->real_escape_string($_POST['email']);
$comment = $conn->real_escape_string($_POST['comment']);
$sql = "INSERT INTO comments (name, email, comment) VALUES ('$name', '$email', '$comment')";
if ($conn->query($sql) === TRUE) {
header("Location: comments.php");
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
显示评论的 PHP 脚本 (comments.php):
<?php
$conn = new mysqli('localhost', 'username', 'password', 'database');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM comments ORDER BY created_at DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<div class='comment'>";
echo "<h3>" . htmlspecialchars($row['name']) . "</h3>";
echo "<p>" . htmlspecialchars($row['comment']) . "</p>";
echo "<small>" . $row['created_at'] . "</small>";
echo "</div>";
}
} else {
echo "No comments yet.";
}
$conn->close();
?>
MySQL 实现评论功能
在 MySQL 中创建评论系统主要涉及表设计和查询优化。
创建带有外键关系的评论表:
CREATE TABLE users (
user_id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
email VARCHAR(100) NOT NULL UNIQUE
);
CREATE TABLE posts (
post_id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
content TEXT NOT NULL,
user_id INT,
FOREIGN KEY (user_id) REFERENCES users(user_id)
);
CREATE TABLE comments (
comment_id INT AUTO_INCREMENT PRIMARY KEY,
content TEXT NOT NULL,
user_id INT,
post_id INT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(user_id),
FOREIGN KEY (post_id) REFERENCES posts(post_id)
);
查询特定文章的所有评论:
SELECT c.content, u.username, c.created_at
FROM comments c
JOIN users u ON c.user_id = u.user_id
WHERE c.post_id = 1
ORDER BY c.created_at DESC;
计算每篇文章的评论数量:
SELECT p.title, COUNT(c.comment_id) as comment_count
FROM posts p
LEFT JOIN comments c ON p.post_id = c.post_id
GROUP BY p.post_id;
实现嵌套评论系统:
CREATE TABLE nested_comments (
comment_id INT AUTO_INCREMENT PRIMARY KEY,
content TEXT NOT NULL,
user_id INT,
post_id INT,
parent_id INT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(user_id),
FOREIGN KEY (post_id) REFERENCES posts(post_id),
FOREIGN KEY (parent_id) REFERENCES nested_comments(comment_id)
);
查询嵌套评论及其回复:
WITH RECURSIVE comment_tree AS (
SELECT comment_id, content, user_id, parent_id, 0 AS depth
FROM nested_comments
WHERE parent_id IS NULL AND post_id = 1
UNION ALL
SELECT nc.comment_id, nc.content, nc.user_id, nc.parent_id, ct.depth + 1
FROM nested_comments nc
JOIN comment_tree ct ON nc.parent_id = ct.comment_id
)
SELECT * FROM comment_tree ORDER BY depth, created_at;






