Home >Backend Development >PHP Tutorial >PHP implements the user points system function in the knowledge question and answer website.
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 database qa
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 named index.php
to implement the function of the user points system .
First, we need to connect to the database. Create a db.php
file and add the following code:
<?php $servername = "localhost"; $username = "root"; $password = "password"; $dbname = "qa"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } ?>
Then, introduce the db.php
file in index.php
:
<?php require_once 'db.php'; ?>
Next, we need to implement the user registration function. Create a register.php
file and add the following code:
<?php require_once 'db.php'; if ($_SERVER["REQUEST_METHOD"] === "POST") { $username = $_POST["username"]; $email = $_POST["email"]; $sql = "INSERT INTO user (username, email) VALUES ('$username', '$email')"; if ($conn->query($sql) === TRUE) { echo "User registration successful!"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } } $conn->close(); ?>
Add the HTML form for user registration in index.php
, the code is as follows:
<form action="register.php" method="POST"> <input type="text" name="username" placeholder="Username" required /> <input type="email" name="email" placeholder="Email" required /> <button type="submit">Register</button> </form>
After the user successfully registers, we need to add points to the user. Add the following code in the register.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 . "<br>" . $conn->error; }
Next, we need to implement the user login function. Create a login.php
file and add the following code:
<?php require_once 'db.php'; if ($_SERVER["REQUEST_METHOD"] === "POST") { $username = $_POST["username"]; $sql = "SELECT * FROM user WHERE username = '$username'"; $result = $conn->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 in index.php
, the code is as follows:
<form action="login.php" method="POST"> <input type="text" name="username" placeholder="Username" required /> <button type="submit">Login</button> </form>
Finally, we need to implement the user's profile page, which is the profile.php
file. Add the following code:
<?php require_once 'db.php'; session_start(); $user_id = $_SESSION["user_id"]; $sql = "SELECT * FROM user WHERE user_id = $user_id"; $result = $conn->query($sql); if ($result->num_rows > 0) { $user = $result->fetch_assoc(); echo "Username: " . $user["username"] . "<br>"; echo "Email: " . $user["email"] . "<br>"; $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"] . "<br>"; } } $conn->close(); ?>
Add the following code in the profile.php
file to display the user's profile and points information:
<?php session_start(); if (!isset($_SESSION["user_id"])) { header("Location: index.php"); exit; } require_once 'db.php'; ?> <h1>User Profile</h1> <a href="logout.php">Logout</a> <?php require_once 'profile.php'; ?>
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!