-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheckout.php
50 lines (42 loc) · 1.55 KB
/
checkout.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
<?php
session_start();
require_once "./functions/database_functions.php";
// print out header here
$title = "Checking out";
require "./template/menu.php";
$userId=$_SESSION['id'];
$conn = db_connect();
if(isset($_SESSION['cart']) && (array_count_values($_SESSION['cart']))){
$date = date("Y-m-d H:i:s");
insertIntoOrder($conn, $userId, $_SESSION['total_price'], $date);
// take orderid from order to insert order items
$orderid = getOrderId($conn, $userId);
foreach($_SESSION['cart'] as $isbn => $qty){
$bookprice = getbookprice($isbn);
$query = "INSERT INTO order_items VALUES
('$orderid', '$isbn', '$bookprice', '$qty')";
$result = mysqli_query($conn, $query);
$bookQty = getBookQuantityFromInventory($conn,$isbn);
$newQty=$bookQty-$qty;
updateBookQunatityInInventory($conn,$isbn,$newQty);
if(!$result){
echo "Insert value false!" . mysqli_error($conn2);
exit;
}
}
deleteAllBooksInCart($userId);
session_unset();
$_SESSION["id"]=$userId;
?>
<p class="lead text-success">Your order has been processed sucessfully. Your order id is <?php echo $orderid?>
Your cart has been empty.</p>
<?php
if(isset($conn)){
mysqli_close($conn);
}
//require_once "./template/footer.php";
} else {
echo "<p class=\"text-warning\">Your cart is empty! Please make sure you add some books in it!</p>";
}
//require_once "./template/footer.php";
?>