-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplace_order.php
65 lines (52 loc) · 2.01 KB
/
place_order.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
<?php
session_start();
include 'connection/config.php';
$_SESSION['user_id'] = 1;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (!isset($_SESSION['user_id'])) {
header("Location:place_order.php?status=login_to_place_order_");
exit;
}
//fetch book quantity from the database
// if($result = $mysqli->query("SELECT stock AS book_quantity FROM books where id = $")){
// if($result){
// $row = $result->fetch_assoc();
// $book_quantity = $row['book_quantity'];
// if($book_quantity <= 0 ){
// header("Location: place_order?error=out_of_stock");
// exit();
// }
// }
// $result->close();
// }
if (!empty($_SESSION['cart'])) {
$cart_items = $_SESSION['cart'];
$ids = implode(',', array_map('intval', $cart_items));
// Fetch products from the database
$sql = "SELECT * FROM books WHERE id IN ($ids)";
$result = $mysqli->query($sql);
if ($result) {
$total_price = 0;
while ($row = $result->fetch_assoc()) {
$total_price += $row['price'];
}
$user_id = $_SESSION['user_id'];
$order_date = date("Y-m-d H:i:s");
$status = 'Pending';
//fetch stock from the
// Insert order into database
$stmt = $mysqli->prepare("INSERT INTO orders (user_id, order_date, total_price, status) VALUES (?, ?, ?, ?)");
$stmt->bind_param("isss", $user_id, $order_date, $total_price, $status);
if ($stmt->execute()) {
unset($_SESSION['cart']);
echo "<script>alert('Order placed successfully!'); window.location.href = 'products.php';</script>";
} else {
echo "Failed to place order: " . $mysqli->error;
}
$stmt->close();
}
} else {
echo "<script>alert('Your cart is empty!'); window.location.href = 'cart.php';</script>";
}
}
?>