Home  >  Article  >  Backend Development  >  Second-hand recycling website developed by PHP provides account balance cashback function

Second-hand recycling website developed by PHP provides account balance cashback function

WBOY
WBOYOriginal
2023-07-02 14:49:361285browse

The second-hand recycling website developed by PHP provides the account balance cashback function

With the widespread circulation of second-hand goods, second-hand recycling websites are becoming more and more popular and popular. In order to increase user stickiness and activity, many second-hand recycling websites have begun to provide account balance cashback functions. This article will discuss how to use PHP to develop a second-hand recycling website and add an account balance cashback function.

First, we need to create a database to store the user's account balance information. Suppose we have a table named "users" that contains the following fields: id (user ID), username (user name), and balance (account balance). The following is the SQL statement to create the table:

CREATE TABLE users (
    id INT(11) AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) NOT NULL,
    balance DECIMAL(10,2) NOT NULL DEFAULT 0.00
);

Next, we need to display the user's account balance on the website's user interface. Suppose we use a file named "balance.php" to display the user's account balance. Here is a simple code example:

<?php
session_start(); // 启动会话

// 检查用户是否已登录
if (!isset($_SESSION['username'])) {
    header("Location: login.php");
    exit;
}

// 获取当前用户的用户名
$username = $_SESSION['username'];

// 连接到数据库
$servername = "localhost";
$username = "数据库用户名";
$password = "数据库密码";
$dbname = "数据库名称";

$conn = new mysqli($servername, $username, $password, $dbname);

// 检查数据库连接是否成功
if ($conn->connect_error) {
    die("数据库连接失败:" . $conn->connect_error);
}

// 查询当前用户的账户余额
$sql = "SELECT balance FROM users WHERE username = '$username'";
$result = $conn->query($sql);

// 检查查询结果
if ($result->num_rows > 0) {
    $row = $result->fetch_assoc();
    $balance = $row['balance'];
    echo "您的账户余额:" . $balance;
} else {
    echo "未找到用户信息";
}

// 关闭数据库连接
$conn->close();
?>

The above code first checks whether the user is logged in and gets the current user's username. name. The code then connects to the database and executes a query to get the current user's account balance. Finally, the code displays the user's account balance. Users can easily view their account balances as they browse the website.

Next, we need to add the account balance cashback function. Suppose we have a file named "cashback.php" to handle account balance cashback requests. Here is a simple code example:

<?php
session_start(); // 启动会话

// 检查用户是否已登录
if (!isset($_SESSION['username'])) {
    header("Location: login.php");
    exit;
}

// 获取当前用户的用户名和返现金额
$username = $_SESSION['username'];
$cashbackAmount = $_POST['cashbackAmount'];

// 连接到数据库
$servername = "localhost";
$username = "数据库用户名";
$password = "数据库密码";
$dbname = "数据库名称";

$conn = new mysqli($servername, $username, $password, $dbname);

// 检查数据库连接是否成功
if ($conn->connect_error) {
    die("数据库连接失败:" . $conn->connect_error);
}

// 更新当前用户的账户余额
$sql = "UPDATE users SET balance = balance + $cashbackAmount WHERE username = '$username'";
if ($conn->query($sql) === TRUE) {
    echo "账户余额返现成功";
} else {
    echo "账户余额返现失败:" . $conn->error;
}

// 关闭数据库连接
$conn->close();
?>

The above code first checks whether the user is logged in and obtains the current user's Username and cashback amount. The code then connects to the database and performs an update operation to update the current user's account balance. Finally, the code displays information about the success or failure of the cashback.

Through the above code example, we can implement a second-hand recycling website and provide users with an account balance cashback function. Users can easily check their account balance and enjoy cash back offers when certain conditions are met. This will increase user satisfaction and loyalty and further promote the development of second-hand recycling websites.

The above is the detailed content of Second-hand recycling website developed by PHP provides account balance cashback function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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