Home >Backend Development >PHP Tutorial >How to use PHP to develop the group purchasing function of WeChat mini program?
How to use PHP to develop the group purchasing function of WeChat mini program?
With the rapid development of WeChat mini programs, group buying has become an important way for many merchants to attract consumers. For programmers who develop group buying functions, how to use PHP to implement this function is a key issue. This article will introduce how to use PHP to develop the group purchasing function of WeChat mini program and provide specific code examples.
Group purchase product table (group_buy_goods):
Type | Description | |
---|---|---|
int(11) | Product ID | |
varchar(100) | Product name | |
decimal(10,2) | Product price | |
int(11) | Quantity of items | |
datetime | Group purchase start time | |
datetime | Group purchase end time | |
datetime | Creation time | |
datetime | Update time |
Type | Description | |
---|---|---|
int(11) | Order ID | |
int(11) | Product ID | |
int(11) | User ID | |
#int(11) | Quantity of goods | |
decimal(10,2 ) | Total order price | ##created_at |
Order creation time |
<?php // 连接数据库 $conn = mysqli_connect("localhost", "root", "", "your_database_name"); if (!$conn) { die("数据库连接失败: " . mysqli_connect_error()); } // 获取团购商品列表 $sql = "SELECT * FROM group_buy_goods"; $result = mysqli_query($conn, $sql); $goodsList = []; if (mysqli_num_rows($result) > 0) { while ($row = mysqli_fetch_assoc($result)) { $goodsList[] = $row; } } // 输出结果 header('Content-Type: application/json'); echo json_encode($goodsList); ?>
<?php // 连接数据库 $conn = mysqli_connect("localhost", "root", "", "your_database_name"); if (!$conn) { die("数据库连接失败: " . mysqli_connect_error()); } // 获取用户ID和商品ID $userId = $_POST['userId']; $goodsId = $_POST['goodsId']; // 获取商品信息 $sql = "SELECT * FROM group_buy_goods WHERE id = '$goodsId'"; $result = mysqli_query($conn, $sql); $goods = mysqli_fetch_assoc($result); if ($goods) { // 检查商品库存是否充足 if ($goods['quantity'] > 0) { // 生成订单 $quantity = 1; $total = $goods['price'] * $quantity; $sql = "INSERT INTO group_buy_order (goods_id, user_id, quantity, total, created_at) VALUES ('$goodsId', '$userId', '$quantity', '$total', NOW())"; if (mysqli_query($conn, $sql)) { // 更新商品库存 $sql = "UPDATE group_buy_goods SET quantity = quantity - 1 WHERE id = '$goodsId'"; mysqli_query($conn, $sql); echo "下单成功"; } else { echo "下单失败"; } } else { echo "商品库存不足"; } } else { echo "商品不存在"; } ?>
// 获取团购商品列表 wx.request({ url: 'https://your_domain/get_goods_list.php', success: function(res) { var goodsList = res.data; console.log(goodsList); // 在页面中展示团购商品 } }); // 下单 wx.request({ url: 'https://your_domain/place_order.php', method: 'POST', data: { userId: 'your_user_id', goodsId: 'your_goods_id' }, success: function(res) { console.log(res.data); // 下单成功提示 } });
The above is the detailed content of How to use PHP to develop the group purchasing function of WeChat mini program?. For more information, please follow other related articles on the PHP Chinese website!