-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_order.php
52 lines (39 loc) · 1.29 KB
/
process_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
<?php
session_start();
// Store the variables from GET
$total = $_GET['bill_total'];
$product_ids = $_GET['product_ids'];
// Require the database connection
require_once "db_conn.php";
// Insert order into the orders table
$user_id = $_SESSION['id'];
$order_date = date('Y-m-d H:i:s');
// Prepare the SQL statement
$sql = "INSERT INTO orders (user_id, order_date, total) VALUES (?, ?, ?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param("iss", $user_id, $order_date, $total);
// Execute the statement
$stmt->execute();
// Get the auto-incremented order_id
$order_id = $stmt->insert_id;
// Insert products into the order_items table
foreach ($product_ids as $product_id) {
// Prepare the SQL statement
$sql = "INSERT INTO order_items (order_id, product_id) VALUES (?, ?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ii", $order_id, $product_id);
// Execute the statement
$stmt->execute();
}
// Empty the SESSION array called cart
$_SESSION['cart'] = array();
// Remove records from the cart_items table where user_id = $user_id
$sql = "DELETE FROM cart_items WHERE user_id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $user_id);
$stmt->execute();
// Close the statement and database connection
$stmt->close();
$conn->close();
header("Location: payment_success.html");
?>