-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchekcoutcartdelete.txt
83 lines (68 loc) · 3.18 KB
/
chekcoutcartdelete.txt
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
session_start();
include 'php/config.php';
// Ensure user is logged in
if (!isset($_SESSION['ID'])) {
header("Location: login.php");
exit();
}
$user_id = $_SESSION['ID']; // Logged-in user's ID
// Ensure POST data is set and sanitize it
$delivery_method = isset($_POST['deliveryMethod']) ? $_POST['deliveryMethod'] : '';
$address = isset($_POST['address']) ? $_POST['address'] : '';
$delivery_date = isset($_POST['date']) ? $_POST['date'] : '';
$email = isset($_POST['email']) ? $_POST['email'] : '';
$phone_num = isset($_POST['phone_num']) ? $_POST['phone_num'] : '';
$order_summary = isset($_POST['orderSummary']) ? json_decode($_POST['orderSummary'], true) : []; // Decode JSON data into PHP array
// Prepare a log entry for debugging (optional)
$log_entry = "[" . date("Y-m-d H:i:s") . "] Received POST data: " . print_r($_POST, true) . "\n";
file_put_contents('checkout_log.txt', $log_entry, FILE_APPEND);
try {
$con->begin_transaction(); // Begin transaction
// Check if the order summary is not empty
if (empty($order_summary)) {
throw new Exception("Order summary is empty.");
}
foreach ($order_summary as $item) {
if (empty($item['name']) || empty($item['quantity']) || empty($item['price'])) {
throw new Exception("Invalid item data.");
}
$product_name = $item['name'];
$quantity = $item['quantity'];
$total_price = $item['price'] * $quantity;
// Query to get product_id from product name
$stmt = $con->prepare("SELECT product_id FROM products WHERE name = ?");
$stmt->bind_param("s", $product_name);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 1) {
$product = $result->fetch_assoc();
$product_id = $product['product_id'];
// Insert order into the database
$stmt_order = $con->prepare("
INSERT INTO orders (user_id, product_id, delivery_option, qnty, address, delivery_date, total)
VALUES (?, ?, ?, ?, ?, ?, ?)
");
$stmt_order->bind_param("iissssd", $user_id, $product_id, $delivery_method, $quantity, $address, $delivery_date, $total_price);
$stmt_order->execute();
// Update product quantity in stock
$stmt_update = $con->prepare("UPDATE products SET stock = stock - ? WHERE product_id = ?");
$stmt_update->bind_param("ii", $quantity, $product_id);
$stmt_update->execute();
// Delete item from cart
$stmt_deletecart = $con->prepare("DELETE FROM cart WHERE product_id = ? AND user_id = ?");
$stmt_deletecart->bind_param("ii", $product_id, $user_id);
$stmt_deletecart->execute();
} else {
throw new Exception("Product not found: $product_name");
}
}
$con->commit(); // Commit the transaction
// Redirect to a success page
header("Location: mainpage.php");
exit();
} catch (Exception $e) {
$con->rollback(); // Rollback the transaction in case of error
echo "Failed to process order: " . $e->getMessage();
}
?>