PHP implements the user points system function in the knowledge question and answer website.

WBOY
Release: 2023-07-02 12:26:02
Original
1371 people have browsed it

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

Next, we create a PHP file namedindex.phpto implement the function of the user points system .

First, we need to connect to the database. Create adb.phpfile and add the following code:

connect_error) { die("Connection failed: " . $conn->connect_error); } ?>
Copy after login

Then, introduce thedb.phpfile inindex.php:

Copy after login

Next, we need to implement the user registration function. Create aregister.phpfile and add the following code:

query($sql) === TRUE) { echo "User registration successful!"; } else { echo "Error: " . $sql . "
" . $conn->error; } } $conn->close(); ?>
Copy after login

Add the HTML form for user registration inindex.php, the code is as follows:

Copy after login

After the user successfully registers, we need to add points to the user. Add the following code in theregister.phpfile:

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

Next, we need to implement the user login function. Create alogin.phpfile 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(); ?>
Copy after login

Add the user login HTML form inindex.php, the code is as follows:

Copy after login

Finally, we need to implement the user's profile page, which is theprofile.phpfile. 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(); ?>
Copy after login

Add the following code in theprofile.phpfile to display the user's profile and points information:

 

User Profile

Logout
Copy after login

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!

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
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!