PHP Development Practice: Using PHP and MySQL to Implement Article Comment Function
Introduction:
In the era of modern technological development, the Internet has become an important channel for people to obtain information and communicate. On the Internet, various websites and social platforms provide user comment functions, allowing users to evaluate and communicate with article content. This article will introduce how to use PHP and MySQL to implement a simple but fully functional article comment function.
1. Environment preparation
Before starting, make sure you have correctly installed the following environment:
2. Create a database table
First, we need to create a database table to store comment information. We can create tables using the MySQL command line tool or graphical tools such as phpMyAdmin. The following is an example SQL statement:
CREATE TABLE comments (
id INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, article_id INT(11) NOT NULL, username VARCHAR(50) NOT NULL, content TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
The table contains the following fields:
3. Write PHP code
Let’s write the PHP code to implement the article comment function. First, create a comment.php file.
e71d1a9d5e0ad99ddb829ed321237e87connect_error) {
die("数据库连接失败: " . $conn->connect_error);
}
// Process form submission
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$article_id = $_POST["article_id"]; $username = $_POST["username"]; $content = $_POST["content"]; // 插入评论数据到数据库 $sql = "INSERT INTO comments (article_id, username, content) VALUES ($article_id, '$username', '$content')"; if ($conn->query($sql) === TRUE) { echo "评论成功"; } else { echo "评论失败: " . $conn->error; }
}
// Get Published comments
$sql = "SELECT * FROM comments";
$result = $conn->query($sql);
// Display comments
if ($ result->num_rows > 0) {
while($row = $result->fetch_assoc()) { echo "用户名:" . $row["username"]. " - 内容:" . $row["content"]. " - 时间:" . $row["created_at"]. "<br>"; }
} else {
echo "暂无评论";
}
// Close the database connection
$conn->close() ;
?>
4. Create an article page
In your website, create an article details page. Include a comment form and published comments on the page.
2a562b58478453b7c571c2d85e62fe7e
<input type="hidden" name="article_id" value="1"> <input type="text" name="username" placeholder="用户名"><br> <textarea name="content" placeholder="写下您的评论"></textarea><br> <input type="submit" value="提交评论">
f5a47148e367a6035fd7a2faa965022e
684271ed9684bde649abda8831d4d355Published comments: 39528cedfa926ea0c01e69ef5b2ea9b0
5bfaddac162324a24d00d1f3c7e875fc
5. Use and Improvement
The above code implements the basic article comment function, you can according to your own Modify and expand according to your needs. For example, you can add user login and permission verification to restrict users to only modify and delete their own comments.
In addition, in order to improve the user experience, you can also use Ajax technology to achieve a refresh-free effect of comment submission and display.
6. Summary
This article introduces how to use PHP and MySQL to implement the article comment function. By creating a database table and writing PHP code, we implemented a simple but fully functional comment system. I hope this article provides some reference and help for you to implement similar functions in PHP development.
The above is the detailed content of PHP development practice: using PHP and MySQL to implement article comment function. For more information, please follow other related articles on the PHP Chinese website!