-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuy.php
78 lines (60 loc) · 1.72 KB
/
buy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
require __DIR__. '/__db_connect.php';
// 先確認是否已登入
if(! isset($_SESSION['loginUser'])) {
header('Location: ./');
exit;
}
// 購物車內是否有產品
if(empty($_SESSION['cart'])) {
header('Location: product-list.php');
exit;
}
$keys = array_keys($_SESSION['cart']);
$sql = sprintf("SELECT * FROM `products` WHERE `sid` IN (%s)",
implode(',', $keys));
$stmt = $pdo->query($sql);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$dict = array();
$totalPrice = 0;
foreach($rows as $r){
$r['qty'] = $_SESSION['cart'][$r['sid']]; // 把數量放進去
$dict[$r['sid']] = $r;
$totalPrice += $r['qty'] * $r['price'];
}
//header('Content-Type: text/plain');
//print_r($dict);
// 寫入 orders 資料表
$o_sql = "INSERT INTO `orders`(`member_sid`, `amount`, `order_date`) VALUES (?, ?, NOW())";
$o_stmt = $pdo->prepare($o_sql);
$o_stmt->execute([
$_SESSION['loginUser']['sid'],
$totalPrice
]);
$order_sid = $pdo->lastInsertId(); // 拿到最近新增一筆的 PK
//echo $order_sid;
// 寫入 order_details 資料表
$od_sql = "INSERT INTO `order_details`(
`order_sid`, `product_sid`, `price`, `quantity`
) VALUES (?, ?, ?, ?)";
$od_stmt = $pdo->prepare($od_sql);
foreach($keys as $k){
$od_stmt->execute([
$order_sid,
$k,
$dict[$k]['price'],
$dict[$k]['qty'],
]);
}
unset($_SESSION['cart']); // 清除購物車內容
?>
<?php include __DIR__. '/__html_head.php' ?>
<?php include __DIR__. '/__nav.php' ?>
<div class="container">
<div class="alert alert-success" role="alert">
感謝訂購
</div>
</div>
<?php include __DIR__. '/__footer.php' ?>
<?php include __DIR__. '/__script.php' ?>
<?php include __DIR__. '/__html_end.php' ?>