Home  >  Article  >  Backend Development  >  PHP development: How to implement article comment function

PHP development: How to implement article comment function

WBOY
WBOYOriginal
2023-09-21 09:24:212019browse

PHP development: How to implement article comment function

PHP development: How to implement the article comment function, specific code examples are required

Introduction:
With the development of the Internet, more and more websites need to have The comment function allows users to discuss and interact with articles. As a commonly used server-side language, PHP can easily implement the article comment function. This article will introduce how to use PHP to implement the article comment function and provide specific code examples.

1. Create a database table
Before using PHP to implement the article comment function, you first need to create a database table to store comment-related data. The following is an example database table structure:

CREATE TABLE comments (

`id` int(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
`article_id` int(11) UNSIGNED,
`name` varchar(255),
`email` varchar(255),
`content` text,
`created_at` datetime,
`updated_at` datetime

);

The table contains the following fields: id (comments unique identifier), article_id (unique identifier of the article), name (commenter name), email (commenter email address), content (comment content), created_at (creation time), updated_at (updated time).

2. Display article comments
It is very simple to use PHP to display article comments. First, we need to call a PHP script in the article page to get the comment data from the database and render it on the page. The following is an example PHP code:

// Connect to the database
$mysqli = new mysqli("localhost", "username", "password", "database") ;

// Check whether the connection is successful
if ($mysqli->connect_error) {

die("数据库连接失败:" . $mysqli->connect_error);

}

// Query comment data
$sql = "SELECT * FROM comments WHERE article_id = 'Unique identification of the article'";
$result = $mysqli->query($sql);

// Check whether the query is successful
if ($result->num_rows > 0) {

// 输出评论数据
while ($row = $result->fetch_assoc()) {
    echo "<p>评论者姓名:" . $row['name'] . "</p>";
    echo "<p>评论内容:" . $row['content'] . "</p>";
    echo "<hr>";
}

} else {

echo "暂无评论";

}

// Close the database connection
$mysqli->close ();
?>

This code first establishes a connection with the database, then obtains the comment data through query, and finally renders the data to the page. If the query is successful, the name and content of each comment will be output in a loop; if the query fails, "No comments yet" will be displayed.

3. Add comment form
In addition to displaying existing comments, we also need to provide a form so that users can add new comments. The following is an example form code:

<input type="hidden" name="article_id" value="文章的唯一标识">
<label for="name">姓名:</label>
<input type="text" name="name" id="name" required><br>
<label for="email">邮箱:</label>
<input type="email" name="email" id="email" required><br>
<label for="content">评论内容:</label>
<textarea name="content" id="content" rows="5" required></textarea><br>
<input type="submit" value="提交评论">

The The form contains three input fields: name, email address and comment content. Users need to fill in this information to submit comments. The form's submission target is "add_comment.php".

4. Processing comment submission
Finally, we need to write a PHP script to handle comment submission. The following is an example "add_comment.php" code:

// Get comment-related parameters
$article_id = $_POST['article_id'];
$ name = $_POST['name'];
$email = $_POST['email'];
$content = $_POST['content'];

// Connect to database
$mysqli = new mysqli("localhost", "username", "password", "database");

// Check whether the connection is successful
if ($mysqli->connect_error) {

die("数据库连接失败:" . $mysqli->connect_error);

}

// Insert comment data
$sql = "INSERT INTO comments (article_id, name, email, content, created_at, updated_at)

    VALUES ('$article_id', '$name', '$email', '$content', NOW(), NOW())";

if ( $mysqli->query($sql) === TRUE) {

echo "评论提交成功";

} else {

echo "评论提交失败:" . $mysqli->error;

}

// Close the database connection
$mysqli ->close();
?>

This code first gets the comment-related parameters from the form, and then inserts these parameters into the database. If the insertion is successful, "Comment submitted successfully" will be displayed "; If the insertion fails, the reason for the failure will be displayed.

Conclusion:
Through the introduction of this article, we have learned how to use PHP to implement the article comment function. By creating a database table, displaying article comments, and adding comments Forms and processing comment submissions, we can easily add comment functionality to the website. I hope this article can be helpful to PHP developers.

Note: The sample code in this article is for reference only, and the specific situation needs to be based on the actual project Make adjustments and modifications.

The above is the detailed content of PHP development: How to implement article comment function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn