Home >Backend Development >PHP Tutorial >Get started quickly: Develop a simple yet powerful shopping mall coupon with PHP

Get started quickly: Develop a simple yet powerful shopping mall coupon with PHP

WBOY
WBOYOriginal
2023-09-11 10:49:49850browse

快速上手:通过 PHP 开发一个简单但功能强大的商场优惠券

Get started quickly: Develop a simple but powerful shopping mall coupon through PHP

With the continuous development of e-commerce, shopping mall coupons have become an important way to attract users to consume. One of the ways. Developing a simple but powerful shopping mall coupon system is crucial for e-commerce platforms. This article will introduce how to use PHP for development and quickly get started creating a reliable and efficient shopping mall coupon system to meet users' shopping needs.

1. Project preparation environment

Before starting, we need to prepare the corresponding environment for the development project. First, make sure you have installed the PHP runtime environment and MySQL database. Secondly, choose an integrated development environment suitable for development, such as PHPStorm, which provides powerful code editing and debugging functions, which can greatly improve development efficiency.

2. Database design

The core of the shopping mall coupon system is database design. We need to create two tables: user table and coupon table.

  1. User table design

The user table is used to store the user's basic information and account balance. The specific fields are as follows:

  • user_id: user ID, primary key
  • username: username
  • password: password
  • balance: account balance
  1. Coupon table design

The coupon table is used to store coupon-related information. The specific fields are as follows:

  • coupon_id: Coupon ID, primary key
  • coupon_code: Coupon code
  • discount: Discount amount
  • threshold: Use Threshold
  • start_time: Validity start time
  • end_time: Validity end time
  • is_used: Whether it has been used

3. Project development steps

  1. Create database

First, create a new database in the MySQL database and name it "coupon_system". Then, create two tables using the following SQL statements:

CREATE TABLE users (
  user_id INT PRIMARY KEY AUTO_INCREMENT,
  username VARCHAR(50) NOT NULL,
  password VARCHAR(50) NOT NULL,
  balance DECIMAL(10, 2) DEFAULT 0
);

CREATE TABLE coupons (
  coupon_id INT PRIMARY KEY AUTO_INCREMENT,
  coupon_code VARCHAR(50) NOT NULL,
  discount DECIMAL(10, 2) NOT NULL,
  threshold DECIMAL(10, 2) DEFAULT 0,
  start_time DATETIME,
  end_time DATETIME,
  is_used TINYINT(1) DEFAULT 0
);
  1. Creating a database connection

Next, we need to create a database connection in PHP. Add the following code to your project's main file:

<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "coupon_system";

$conn = mysqli_connect($servername, $username, $password, $dbname);

if (!$conn) {
    die("连接数据库失败: " . mysqli_connect_error());
}
?>

Be sure to replace $username and $password with the correct database username and password.

  1. Create registration and login functions

We need to create user registration and login functions. Create a register.php file to implement user registration logic. The following is a simple example:

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = $_POST['username'];
    $password = $_POST['password'];

    $sql = "INSERT INTO users (username, password) VALUES ('$username', '$password')";

    if (mysqli_query($conn, $sql)) {
        echo "注册成功!";
    } else {
        echo "注册失败:" . mysqli_error($conn);
    }
}
?>
<h1>用户注册</h1>
<form method="POST">
    <input type="text" name="username" placeholder="用户名" required>
    <input type="password" name="password" placeholder="密码" required>
    <button type="submit">注册</button>
</form>

Similarly, create a login.php file to implement the user login logic. The following is a simple example:

<?php
session_start();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = $_POST['username'];
    $password = $_POST['password'];

    $sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";
    $result = mysqli_query($conn, $sql);

    if (mysqli_num_rows($result) == 1) {
        $_SESSION['username'] = $username;
        echo "登录成功!";
    } else {
        echo "用户名或密码错误!";
    }
}
?>
<h1>用户登录</h1>
<form method="POST">
    <input type="text" name="username" placeholder="用户名" required>
    <input type="password" name="password" placeholder="密码" required>
    <button type="submit">登录</button>
</form>
  1. Create coupon function

Create a coupons.php file to receive and use coupons Function. The following is a simple example:

<?php
session_start();

if (!isset($_SESSION['username'])) {
    header("Location: login.php");
    exit;
}

$username = $_SESSION['username'];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $coupon_code = $_POST['coupon_code'];

    $sql = "SELECT * FROM coupons WHERE coupon_code='$coupon_code' AND is_used=0";
    $result = mysqli_query($conn, $sql);

    if (mysqli_num_rows($result) == 1) {
        $coupon = mysqli_fetch_assoc($result);
        $discount = $coupon['discount'];
        $threshold = $coupon['threshold'];
        $balance = mysqli_fetch_assoc(mysqli_query($conn, "SELECT balance FROM users WHERE username='$username'"))['balance'];

        if ($balance >= $threshold) {
            $new_balance = $balance - $discount;
            mysqli_query($conn, "UPDATE users SET balance=$new_balance WHERE username='$username'");
            mysqli_query($conn, "UPDATE coupons SET is_used=1 WHERE coupon_code='$coupon_code'");

            echo "使用优惠券成功!";
        } else {
            echo "账户余额不足,请先充值!";
        }
    } else {
        echo "优惠券已被使用或不存在!";
    }
}

$sql = "SELECT * FROM coupons WHERE is_used=0";
$result = mysqli_query($conn, $sql);
$coupons = mysqli_fetch_all($result, MYSQLI_ASSOC);
?>
<h1>领取优惠券</h1>
<?php foreach ($coupons as $coupon) { ?>
    <form method="POST">
        <input type="hidden" name="coupon_code" value="<?php echo $coupon['coupon_code']; ?>">
        <h3><?php echo $coupon['coupon_code']; ?></h3>
        <p>折扣金额:<?php echo $coupon['discount']; ?></p>
        <p>使用门槛:<?php echo $coupon['threshold']; ?></p>
        <button type="submit">领取</button>
    </form>
<?php } ?>

The above code implements the function of users receiving and using coupons. Users can only receive unused coupons and can make deductions when the usage threshold is reached.

4. Summary

Through the above steps, we quickly developed a simple but powerful shopping mall coupon system. From database design to function implementation, we provide users with registration, login, coupon collection and use functions. This system can be used as the basic function of the e-commerce platform to provide users with a better shopping experience. Of course, there is still a lot of room for improvement and expansion, which can be optimized and perfected according to actual needs. I hope this article will be helpful for you to learn PHP development and build a coupon system!

The above is the detailed content of Get started quickly: Develop a simple yet powerful shopping mall coupon with PHP. 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