Use PHP to develop the user comment function in the knowledge question and answer website
With the rapid development of the Internet, the knowledge question and answer website has become an important platform for people to obtain and share knowledge. The user comment function is a crucial component of this type of website, which allows users to discuss questions and answers and put forward their own views and opinions.
This article will introduce how to use PHP to develop the user comment function in a knowledge question and answer website. We will complete this function through the following steps:
First, we need to design a database to store user comments. We can create a data table named comments, containing the following fields:
This data table will be used to store user comment information.
Next, we need to display comments in the question and answer pages. We can add a comment area on the question and answer display page and read the corresponding comments from the database for display.
Code example (PHP MySQL):
<?php // 连接数据库 $mysqli = new mysqli('localhost', 'username', 'password', 'database'); // 检查连接是否成功 if ($mysqli->connect_errno) { die('Connect Error: ' . $mysqli->connect_errno); } // 获取问题或答案的 ID $id = $_GET['id']; // 查询评论 $result = $mysqli->query("SELECT * FROM comments WHERE question_id = $id OR answer_id = $id"); // 循环展示评论 while ($row = $result->fetch_assoc()) { echo '<div class="comment">'; echo '<p>' . $row['content'] . '</p>'; echo '<p>' . $row['created_at'] . '</p>'; echo '</div>'; } // 关闭数据库连接 $mysqli->close(); ?>
In the question and answer display page, through the above code, we can query the comments of the corresponding question or answer from the database and add them Rendered to the page.
In addition to displaying comments, we also need to provide the function for users to add comments. We can add a comment input box and a submit button below the question and answer display page to allow users to enter comments and submit them to the database.
Code example (PHP MySQL):
<?php // 连接数据库 $mysqli = new mysqli('localhost', 'username', 'password', 'database'); // 检查连接是否成功 if ($mysqli->connect_errno) { die('Connect Error: ' . $mysqli->connect_errno); } // 获取用户输入的评论内容 $content = $_POST['content']; // 获取问题或答案的 ID $id = $_GET['id']; // 获取当前用户的 ID(假设用户已经登录) $user_id = $_SESSION['user_id']; // 插入评论数据到数据库 $mysqli->query("INSERT INTO comments (user_id, question_id, answer_id, content) VALUES ($user_id, $id, $id, '$content')"); // 关闭数据库连接 $mysqli->close(); ?>
Through the above code, we can get the comment content entered by the user, the ID of the current question or answer, and the ID of the current user, and combine this information Insert into the comments table of the database.
Summary:
Through the above steps, we can use PHP to develop the user comment function in the knowledge question and answer website. First, we need to design the database table structure of the comments; then, display the comments in the question and answer display page, and provide the function of submitting comments. In this way, users can comment and communicate on the knowledge question and answer website, further enriching and expanding the dissemination of knowledge.
The above is the detailed content of Developed user comment functionality in a trivia website using PHP.. For more information, please follow other related articles on the PHP Chinese website!