Home > Article > Backend Development > Second-hand recycling website developed by PHP provides coupon collection function
The second-hand recycling website developed by PHP provides the coupon collection function
As people’s awareness of environmental protection increases, second-hand recycling is receiving more and more attention. More and more people are paying attention to the value of second-hand items and reducing waste and environmental pollution through recycling. In order to facilitate users and improve recycling efficiency, many second-hand recycling websites have begun to use platforms developed in PHP to provide various functions. Among them, the coupon collection function is one of the important factors that attract users. This article will introduce how to use PHP to develop such a second-hand recycling website.
In order to implement the coupon collection function, we will use PHP language to process the back-end logic and use the MySQL database to store coupon information. Before you begin, you need to make sure that PHP and MySQL are installed on your server. Next, we will divide it into the following steps to implement this function.
Step 1: Create a database table
First, we need to create a table to store coupon information. In the MySQL database, use the following command to create a table named "coupons":
CREATE TABLE `coupons` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `code` VARCHAR(50) NOT NULL, `value` DECIMAL(10,2) NOT NULL, `expiry_date` DATE NOT NULL, PRIMARY KEY (`id`) );
In the above table, the id
field is used to identify the uniqueness of each coupon,# The ##code field is used to store the code of the coupon, the
value field is used to store the value of the coupon, and the
expiry_date field is used to store the expiration date of the coupon.
<?php // 连接到MySQL数据库 $conn = new mysqli('localhost', 'username', 'password', 'database_name'); // 检查连接是否成功 if ($conn->connect_error) { die('数据库连接失败: ' . $conn->connect_error); } // 获取所有的优惠券信息 $sql = "SELECT * FROM coupons"; $result = $conn->query($sql); // 将所有的优惠券信息存储在一个数组中 $coupons = array(); if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { $coupons[] = $row; } } // 关闭数据库连接 $conn->close(); ?>In the above code, we first connect to the MySQL database through the
new mysqli() function. Then, execute the SQL statement to obtain the coupon through the
$conn->query() function, and store the query results in an array through the
fetch_assoc() function. Finally, close the database connection through the
$conn->close() function.
<html> <head> <title>优惠券领取</title> </head> <body> <h1>优惠券列表</h1> <ul> <?php foreach ($coupons as $coupon): ?> <li><?php echo $coupon['code']; ?> - <?php echo $coupon['value']; ?>元</li> <?php endforeach; ?> </ul> </body> </html>In the above code, we have used HTML syntax to create a web page and looped through the coupon array using
foreach through the PHP code. In the loop, we use the
echo function to output the code and value of the coupon.
The above is the detailed content of Second-hand recycling website developed by PHP provides coupon collection function. For more information, please follow other related articles on the PHP Chinese website!