Home > Database > Mysql Tutorial > body text

PHP development practice: using PHP and MySQL to implement article comment function

王林
Release: 2023-07-01 18:40:39
Original
1500 people have browsed it

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:

  1. PHP version 5.6 and above
  2. MySQL database

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
Copy after login

);

The table contains the following fields:

  1. id : Primary key, auto-incremented, used to uniquely identify each comment
  2. article_id: article ID, used to associate comments and corresponding articles
  3. username: commenter’s username
  4. content: Comment content
  5. created_at: Comment creation time

3. Write PHP code
Let’s write the PHP code to implement the article comment function. First, create a comment.php file.

//Connect to the database
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "mydb";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {

die("数据库连接失败: " . $conn->connect_error);
Copy after login

}

// 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;
}
Copy after login

}

// 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"]. "
"; }
Copy after login

} else {

echo "暂无评论";
Copy after login

}

// 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.




Copy after login

Published comments:


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!

Related labels:
source:php.cn
Statement of this Website
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!