-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_order.php
More file actions
67 lines (60 loc) · 2.99 KB
/
Copy pathprocess_order.php
File metadata and controls
67 lines (60 loc) · 2.99 KB
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
<?php
session_start();
include 'DBConn.php'; // 1. Connect to your database
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// 2. Collect User and Shipping Data
$fname = mysqli_real_escape_string($conn, $_POST['fname']);
$lname = mysqli_real_escape_string($conn, $_POST['lname']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$address = mysqli_real_escape_string($conn, $_POST['address']);
$city = mysqli_real_escape_string($conn, $_POST['city']);
$zip = mysqli_real_escape_string($conn, $_POST['zip']);
// Calculate total again for security
$subtotal = 0;
foreach ($_SESSION['cart'] as $id => $qty) {
$id = mysqli_real_escape_string($conn, $id);
$res = mysqli_query($conn, "SELECT price FROM products WHERE id = '$id'");
$item = mysqli_fetch_assoc($res);
$subtotal += ($item['price'] * $qty);
}
$total_amount = $subtotal + 60.00; // Adding shipping
// 3. Insert into the ORDERS table
$order_query = "INSERT INTO orders (total_amount, status) VALUES ('$total_amount', 'pending')";
if (mysqli_query($conn, $order_query)) {
$order_id = mysqli_insert_id($conn); // Get the ID of the order we just created
// 4. Insert each item into the ORDER_ITEMS table
foreach ($_SESSION['cart'] as $id => $qty) {
$id = mysqli_real_escape_string($conn, $id);
$res = mysqli_query($conn, "SELECT price FROM products WHERE id = '$id'");
$item = mysqli_fetch_assoc($res);
$price = $item['price'];
$item_query = "INSERT INTO order_items (order_id, product_id, quantity, price_at_purchase)
VALUES ('$order_id', '$id', '$qty', '$price')";
mysqli_query($conn, $item_query);
}
// 5. Clear the Bag and show success
unset($_SESSION['cart']);
echo "
<!DOCTYPE html>
<html>
<head>
<link href='https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css' rel='stylesheet'>
<style>
body { background: #000; color: #fff; font-family: 'Inter', sans-serif; display: flex; align-items: center; justify-content: center; height: 100vh; text-align: center; }
.success-card { border: 1px solid #333; padding: 60px; }
.btn-home { background: #0678eb; color: #fff; text-decoration: none; padding: 15px 30px; font-weight: 800; text-transform: uppercase; display: inline-block; margin-top: 20px; }
</style>
</head>
<body>
<div class='success-card'>
<h1 class='display-3 fw-black mb-4'>ORDER PLACED</h1>
<p class='lead mb-4'>Thank you, $fname. Your order #$order_id has been received.</p>
<a href='index.php' class='btn-home'>Back to Collection</a>
</div>
</body>
</html>";
} else {
echo "Error: " . mysqli_error($conn);
}
}
?>