How to use PHP to implement user comment function

WBOY
Release: 2023-09-24 09:54:01
Original
3117 people have browsed it

How to use PHP to implement user comment function

How to use PHP to implement user comment function
With the development of the Internet, user comment function has become an essential part of many websites and applications. User comments allow users to share their views and opinions, enrich website content, and provide reference and guidance to other users. In this article, we will introduce how to use PHP language to implement user comment function and provide specific code examples.

1. Create a comment table

First, we need to create a table in the database to store user comments. The structure of the table can be designed according to actual needs, but generally includes fields such as comment content, user name, publishing time, etc. The following is a sample table structure:

CREATE TABLE comments (
    id INT AUTO_INCREMENT PRIMARY KEY,
    content TEXT,
    username VARCHAR(50),
    created_at DATETIME
);
Copy after login

2. Display the comment list

Next, we need to write PHP code to display the comment list. You can put the comment list in an HTML page, use PHP code to read the comment data from the database and dynamically generate the comment list.

<?php
// 连接数据库
$mysqli = new mysqli('localhost', 'username', 'password', 'database');
if ($mysqli->connect_errno) {
    die('连接失败:' . $mysqli->connect_error);
}

// 查询评论数据
$result = $mysqli->query('SELECT * FROM comments ORDER BY created_at DESC');
if (!$result) {
    die('查询失败:' . $mysqli->error);
}

// 循环遍历评论数据,生成评论列表
while ($row = $result->fetch_assoc()) {
    echo '<div>';
    echo '<p>用户名:' . $row['username'] . '</p>';
    echo '<p>评论内容:' . $row['content'] . '</p>';
    echo '<p>发布时间:' . $row['created_at'] . '</p>';
    echo '</div>';
}

// 释放资源
$result->free();
$mysqli->close();
?>
Copy after login

3. Add comments

Add a comment form to the page so that users can enter comments and submit them. Receive form data in PHP code and insert comment data into database.

<?php
// 连接数据库
$mysqli = new mysqli('localhost', 'username', 'password', 'database');
if ($mysqli->connect_errno) {
    die('连接失败:' . $mysqli->connect_error);
}

// 检查是否有提交评论的表单数据
if (isset($_POST['username']) && isset($_POST['content'])) {
    // 准备插入评论数据的SQL语句
    $stmt = $mysqli->prepare('INSERT INTO comments (content, username, created_at) VALUES (?, ?, NOW())');
    $stmt->bind_param('ss', $_POST['content'], $_POST['username']);

    // 执行SQL语句
    if (!$stmt->execute()) {
        die('插入失败:' . $stmt->error);
    }

    // 关闭语句
    $stmt->close();
}

// 释放资源
$mysqli->close();
?>

<form action="" method="post">
    <input type="text" name="username" placeholder="用户名" required>
    <textarea name="content" placeholder="评论内容" required></textarea>
    <button type="submit">发表评论</button>
</form>
Copy after login

The above code demonstrates how to receive the comment content entered by the user, insert it into the database, and display the comment list on the page. When the user submits a comment, the page will refresh and display the latest comment content. More functions can be added according to actual needs, such as paging display of comments, administrator review, etc.

Through the above tutorial, we learned how to use PHP to implement the user comment function. Hope this article can be helpful to you.

The above is the detailed content of How to use PHP to implement user 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 admin@php.cn
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!