-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckout.php
More file actions
78 lines (64 loc) · 3.19 KB
/
checkout.php
File metadata and controls
78 lines (64 loc) · 3.19 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
68
69
70
71
72
73
74
75
76
77
78
<?php
include('header.php');
$userId = $_SESSION['id']; // Get the user ID from the session
// Fetch existing addresses for the user
$addressQuery = "SELECT * FROM user_addresses WHERE u_id = $userId";
$addresses = mysqli_query($conn, $addressQuery);
// Handle form submission
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$address = $_POST['address']; // Address the user selects or enters
$payment_method = $_POST['payment_method']; // Payment method selected by the user
// Insert a new address if the user has selected "Add New Address"
if (isset($_POST['new_address'])) {
$insertAddressQuery = "INSERT INTO user_addresses (u_id, address) VALUES ($userId, '$address')";
mysqli_query($conn, $insertAddressQuery);
}
// Insert order into the orders table
$orderQuery = "INSERT INTO orders (u_id, order_date, status) VALUES ($userId, NOW(), 'Pending')";
if (mysqli_query($conn, $orderQuery)) {
$orderId = mysqli_insert_id($conn); // Get the generated order ID
// Fetch cart items for the user
$cartQuery = "SELECT c.p_id, c.quantity, p.price FROM cart c JOIN product p ON c.p_id = p.p_id WHERE c.u_id = $userId";
$cartItems = mysqli_query($conn, $cartQuery);
// Insert each cart item into order_details table
while ($item = mysqli_fetch_assoc($cartItems)) {
$p_id = $item['p_id'];
$quantity = $item['quantity'];
$price = $item['price'];
$orderDetailQuery = "INSERT INTO order_details (order_id, p_id, quantity, price)
VALUES ($orderId, $p_id, $quantity, $price)";
mysqli_query($conn, $orderDetailQuery);
}
// Clear the cart after placing the order
$clearCartQuery = "DELETE FROM cart WHERE u_id = $userId";
mysqli_query($conn, $clearCartQuery);
// Insert into admin_order_request to notify admin
$adminOrderRequestQuery = "INSERT INTO admin_order_request (order_id, u_id, status)
VALUES ($orderId, $userId, 'Pending')";
mysqli_query($conn, $adminOrderRequestQuery);
// Show success message
echo "Order placed successfully!";
} else {
echo "Error placing order: " . mysqli_error($conn);
}
}
?>
<form method="POST">
<h3>Choose Your Address</h3>
<?php if (mysqli_num_rows($addresses) > 0): ?>
<p>Select an Address</p>
<?php while ($address = mysqli_fetch_assoc($addresses)): ?>
<label>
<input type="radio" name="address" value="<?= $address['address']; ?>" required>
<?= $address['address']; ?>
</label><br>
<?php endwhile; ?>
<button type="submit" name="new_address" value="1">Add New Address</button>
<?php else: ?>
<textarea name="address" placeholder="Enter your address" required></textarea><br>
<?php endif; ?>
<h3>Select Payment Method</h3>
<label><input type="radio" name="payment_method" value="COD" checked> Cash on Delivery</label><br>
<label><input type="radio" name="payment_method" value="UPI"> UPI/Net Banking (Demo)</label><br>
<button type="submit" name="place_order">Place Order</button>
</form>