PHP realizes the question followers and expert certification functions in the knowledge question and answer website
With the continuous development of the Internet, the knowledge question and answer website is becoming more and more popular. This kind of website has become a way for people to solve problems and share knowledge. important platform. In order to increase the credibility and activity of the website, many question and answer websites have begun to introduce question followers and expert certification functions. This article will introduce how to use PHP to achieve this function.
1. Design the database structure
First you need to design a database to store user information, questions and answers and other related data. We can create the following tables to implement this function:
2. Implement the follow function
First, we need to provide users with a button or link to follow the issue. When the user clicks the follow button, a PHP function should be triggered that is responsible for inserting the follower ID and question ID into the follow table.
The following is a sample code:
<?php // 获取用户ID和问题ID $userID = $_SESSION['userID']; $questionID = $_GET['questionID']; // 将用户ID和问题ID插入关注表中 $sql = "INSERT INTO follows (userID, questionID) VALUES ('$userID', '$questionID')"; $result = mysqli_query($conn, $sql); if ($result) { echo "关注成功"; } else { echo "关注失败"; } ?>
It should be noted that SESSION is used in the code to obtain the user ID, ensuring that only logged-in users can perform follow operations. In addition, you need to connect to the database and perform insert operations.
3. Implement the expert certification function
In order to implement the expert certification function, we can add a field to the user table to mark whether the user is an expert. For example, we can add a boolean field called isExpert.
When users register or edit their profile, users can choose whether they are an expert. When the user chooses to be an expert, we need to set the isExpert field to true.
The following is a sample code:
<?php // 获取用户ID和专家认证状态 $userID = $_SESSION['userID']; $isExpert = $_POST['isExpert']; // 更新用户表中的isExpert字段 $sql = "UPDATE users SET isExpert = '$isExpert' WHERE userID = '$userID'"; $result = mysqli_query($conn, $sql); if ($result) { echo "专家认证成功"; } else { echo "专家认证失败"; } ?>
It should be noted that POST is used in the code to obtain the expert certification status selected by the user. In addition, you need to connect to the database and perform update operations.
By implementing the above attention and expert certification functions, we can add more interaction and trust mechanisms to the knowledge question and answer website. Users can choose to pay attention to the questions they are interested in and get the latest updates on the questions in a timely manner; expert certification can make users more confident in the answers of experts and improve the credibility and activity of the Q&A website.
The above is the detailed content of PHP implements question attention and authentication on Q&A website. For more information, please follow other related articles on the PHP Chinese website!