-
Notifications
You must be signed in to change notification settings - Fork 4
/
buy.php
60 lines (48 loc) · 1.31 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
<?php
/**
* Created by PhpStorm.
* User: ying-raylu
* Date: 25/12/2016
* Time: 5:20 PM
*/
include __DIR__. '/__connect_db.php';
if(! isset($_SESSION['user'])){
header('Location: login.php');
exit;
}
if(empty($_SESSION['cart'])){
header('Location: product.html');
exit;
}
$sids = array_keys($_SESSION['cart']);
$sql = sprintf("SELECT * FROM `products` WHERE `sid` IN (%s) ", implode(',', $sids));
$result = $mysqli->query($sql);
$p_data = array();
$totalPrice = 0;
while($row=$result->fetch_assoc()){
$row['qty'] = $_SESSION['cart'][$row['sid']][0];
$p_data[$row['sid']] = $row;
$totalPrice += $row['qty']*$row['price'];
}
$sql = "INSERT INTO `orders`(
`member_sid`, `amount`, `order_date`
) VALUES (
{$_SESSION['user']['sid']},
$totalPrice,
NOW()
)";
$mysqli->query($sql);
$order_sid = $mysqli->insert_id; //取得訂單編號
foreach($sids as $sid){
$sql = "INSERT INTO `order_details`(
`order_sid`, `product_sid`, `price`, `quantity`
) VALUES (
$order_sid,
$sid,
{$p_data[$sid]['price']},
{$p_data[$sid]['qty']}
)";
$mysqli->query($sql);
}
unset($_SESSION['cart']); //清空購物車
header('Location: cartshoppingdone.php');