PHP implements the user points system function in the knowledge question and answer website
With the popularity of the knowledge question and answer website, we can often see users actively participating in answering questions and actively sharing their knowledge. In order to stimulate user participation and contribution, many knowledge question and answer websites will introduce user points systems. This article will introduce how to use PHP to implement a simple user points system function.
First of all, we need a database to store the user's points information. Suppose we have two tables: user and score. The user table stores user-related information, and the score table stores user points information. Create databaseqa
and create a table:
CREATE TABLE `user` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `username` VARCHAR(50) NOT NULL, `email` VARCHAR(50) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `score` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `user_id` INT(11) NOT NULL, `score` INT(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Next, we create a PHP file namedindex.php
to implement the function of the user points system .
First, we need to connect to the database. Create adb.php
file and add the following code:
connect_error) { die("Connection failed: " . $conn->connect_error); } ?>
Then, introduce thedb.php
file inindex.php
:
Next, we need to implement the user registration function. Create aregister.php
file and add the following code:
query($sql) === TRUE) { echo "User registration successful!"; } else { echo "Error: " . $sql . "
" . $conn->error; } } $conn->close(); ?>
Add the HTML form for user registration inindex.php
, the code is as follows:
After the user successfully registers, we need to add points to the user. Add the following code in theregister.php
file:
$score = 100; $sql = "INSERT INTO score (user_id, score) VALUES (LAST_INSERT_ID(), '$score')"; if ($conn->query($sql) === TRUE) { echo "Score added successfully!"; } else { echo "Error: " . $sql . "
" . $conn->error; }
Next, we need to implement the user login function. Create alogin.php
file and add the following code:
query($sql); if ($result->num_rows > 0) { $user = $result->fetch_assoc(); session_start(); $_SESSION["user_id"] = $user["user_id"]; $_SESSION["username"] = $user["username"]; header("Location: profile.php"); } else { echo "User not found!"; } } $conn->close(); ?>
Add the user login HTML form inindex.php
, the code is as follows:
Finally, we need to implement the user's profile page, which is theprofile.php
file. Add the following code:
query($sql); if ($result->num_rows > 0) { $user = $result->fetch_assoc(); echo "Username: " . $user["username"] . "
"; echo "Email: " . $user["email"] . "
"; $sql_score = "SELECT * FROM score WHERE user_id = $user_id"; $result_score = $conn->query($sql_score); if ($result_score->num_rows > 0) { $score = $result_score->fetch_assoc(); echo "Score: " . $score["score"] . "
"; } } $conn->close(); ?>
Add the following code in theprofile.php
file to display the user's profile and points information:
User Profile
Logout
At this point, we have successfully implemented Added user points system function. Users can register, log in, and view their points information on the profile page.
Summary: This article uses PHP to implement the user points system function in a knowledge question and answer website. By connecting to the database, registering users, adding points to users, and realizing user login and profile page functions. I hope this article will be helpful to everyone in learning PHP programming and website development.
The above is the detailed content of PHP implements the user points system function in the knowledge question and answer website.. For more information, please follow other related articles on the PHP Chinese website!