1 Star 1 Fork 0

AxxSabrina/基于PHP+Mysql的Cyperbunk2077未来车辆店管理系统设计与开发

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
processCheckout.php 6.46 KB
一键复制 编辑 原始数据 按行查看 历史
AxxSabrina 提交于 2024-10-09 13:40 . 网站代码提交 不附带素材
<?php
header('Content-Type: application/json');
session_start();
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "cyberpunk";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die(json_encode(['success' => false, 'message' => '连接数据库失败: ' . $conn->connect_error]));
}
// 检查用户是否已登录
if (!isset($_SESSION['id'])) {
die(json_encode(['success' => false, 'message' => '用户未登录,请先登录。']));
}
$user_id = $_SESSION['id'];
$purchase_items = isset($_POST['purchase_items']) && is_array($_POST['purchase_items']) ? $_POST['purchase_items'] : [];
$delivery_address = isset($_POST['delivery_address']) && is_array($_POST['delivery_address']) ? $_POST['delivery_address'] : [];
// 确保所有地址字段都存在且非空
if (
!isset($delivery_address['address_line_1']) || empty($delivery_address['address_line_1']) ||
!isset($delivery_address['city']) || empty($delivery_address['city']) ||
!isset($delivery_address['state_province']) || empty($delivery_address['state_province']) ||
!isset($delivery_address['country']) || empty($delivery_address['country']) ||
!isset($delivery_address['postal_code']) || empty($delivery_address['postal_code'])
) {
die(json_encode(['success' => false, 'message' => '缺失或无效的送货地址信息']));
}
if (empty($purchase_items)) {
die(json_encode(['success' => false, 'message' => '未接收到要购买的商品ID。']));
}
// 查询用户余额和商品详情
$sql = "
SELECT u.Money, v.id AS vehicle_id, v.brand, v.price
FROM users u
INNER JOIN vehicle v ON v.id IN (" . implode(',', $purchase_items) . ")
WHERE u.id = ?
";
$stmt = $conn->prepare($sql);
if (!$stmt) {
handleQueryError($conn, "预处理查询用户余额和商品详情 SQL 语句失败");
}
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();
$user_balance = null;
$vehicles_data = [];
while ($row = $result->fetch_assoc()) {
if ($row['Money']) {
$user_balance = $row['Money'];
}
$vehicles_data[$row['vehicle_id']] = [
'brand' => $row['brand'],
'price' => $row['price'],
];
}
$stmt->close();
if ($user_balance === null) {
die(json_encode(['success' => false, 'message' => '用户余额查询失败。']));
}
// 计算商品总价
$total_price = array_sum(array_column($vehicles_data, 'price'));
// 判断余额是否足够
if ($user_balance < $total_price) {
die(json_encode([
'success' => false,
'message' => '余额不足,无法购买所选商品。',
'remaining_balance' => $user_balance,
]));
}
// 扣除用户余额(假设交易成功)
$new_balance = $user_balance - $total_price;
$sql = "UPDATE users SET Money = ? WHERE id = ?";
$stmt = $conn->prepare($sql);
if (!$stmt) {
handleQueryError($conn, "预处理更新用户余额 SQL 语句失败");
}
$stmt->bind_param("di", $new_balance, $user_id);
$stmt->execute();
$affected_rows = $stmt->affected_rows;
$stmt->close();
if ($affected_rows > 0) {
// 删除购物车中的商品
$sql = "DELETE FROM user_cart WHERE user_id = ? AND vehicle_id IN (?)";
$stmt = $conn->prepare($sql);
if (!$stmt) {
handleQueryError($conn, "预处理删除购物车商品 SQL 语句失败");
}
$stmt->bind_param("is", $user_id, implode(',', $purchase_items));
$stmt->execute();
$affected_rows = $stmt->affected_rows;
if ($affected_rows > 0) {
// 插入购买记录到 purchase_records 表
$sql = "INSERT INTO purchase_records (user_id, vehicle_id, purchase_date) VALUES (?, ?, NOW())";
$stmt = $conn->prepare($sql);
if (!$stmt) {
handleQueryError($conn, "预处理插入购买记录 SQL 语句失败");
}
foreach ($purchase_items as $vehicle_id) {
$stmt->bind_param("ii", $user_id, $vehicle_id);
$stmt->execute();
}
// 插入订单数据到 orders 表
$sql = "INSERT INTO orders (user_id, vehicle_id, brand, price, purchase_date, address_line_1, city, state_province, country, postal_code) VALUES (?, ?, ?, ?, NOW(), ?, ?, ?, ?, ?)";
$stmt = $conn->prepare($sql);
if (!$stmt) {
handleQueryError($conn, "预处理插入订单数据 SQL 语句失败");
}
$insertion_errors = []; // 存储插入订单时的错误信息
foreach ($purchase_items as $vehicle_id) {
$vehicle_data = $vehicles_data[$vehicle_id];
$stmt->bind_param(
"iisssssss",
$user_id,
$vehicle_id,
$vehicle_data['brand'],
$vehicle_data['price'],
$delivery_address['address_line_1'],
$delivery_address['city'],
$delivery_address['state_province'],
$delivery_address['country'],
$delivery_address['postal_code']
);
if (!$stmt->execute()) {
$insertion_errors[] = [
'vehicle_id' => $vehicle_id,
'error' => $stmt->error,
];
}
}
$stmt->close();
if (!empty($insertion_errors)) {
$error_messages = array_map(function ($error) {
return "订单插入失败 (车辆ID: {$error['vehicle_id']}): {$error['error']}";
}, $insertion_errors);
die(json_encode([
'success' => false,
'message' => '部分订单插入失败: ' . implode('; ', $error_messages),
'remaining_balance' => $new_balance,
]));
}
$response = [
'success' => true,
'message' => '购买成功,车辆已发车至您的车库中!',
'remaining_balance' => $new_balance,
];
} else {
$response = [
'success' => false,
'message' => '删除购物车商品时发生错误。',
'remaining_balance' => $new_balance,
];
}
} else {
$response = [
'success' => false,
'message' => '更新用户余额时发生错误,请稍后再试。',
'remaining_balance' => $user_balance,
];
}
// 输出 JSON 数据
echo json_encode($response);
$conn->close();
function handleQueryError(mysqli $conn, string $error_message): void
{
$conn_error = $conn->error;
die(json_encode([
'success' => false,
'message' => "$error_message: $conn_error",
]));
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
HTML
1
https://gitee.com/axxsabrina/Cyberpunk2077_Future_Vehicle_Store.git
git@gitee.com:axxsabrina/Cyberpunk2077_Future_Vehicle_Store.git
axxsabrina
Cyberpunk2077_Future_Vehicle_Store
基于PHP+Mysql的Cyperbunk2077未来车辆店管理系统设计与开发
master

搜索帮助