-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlaceOrder.php
58 lines (48 loc) · 1.92 KB
/
PlaceOrder.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
<?php
include "Conx.php";
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Content-Type, Authorization');
header('Access-Control-Allow-Methods: POST, OPTIONS');
header('Content-Type: application/json');
$data = json_decode(file_get_contents('php://input'), true);
if (isset($data['userId'], $data['cartItems'], $data['grandTotal'], $data['address'], $data['cardNumber'], $data['expirationDate'])) {
$user_id = $data['userId'];
$cartItems = $data['cartItems'];
$address = $data['address'];
$card_number = $data['cardNumber'];
$expiration_date = $data['expirationDate'];
$conn->begin_transaction();
try {
foreach ($cartItems as $item) {
$stmt = $conn->prepare(
"INSERT INTO orders (user_id, product_id, price, quantity, totalPrice, image, order_date, Address, card_number, expiration_date)
VALUES (?, ?, ?, ?, ?, ?, NOW(), ?, ?, ?)"
);
$stmt->bind_param(
"iididssss", // Updated types to match the columns
$user_id,
$item['product_id'],
$item['price'],
$item['quantity'],
$item['totalPrice'],
$item['image'],
$address,
$card_number,
$expiration_date
);
if (!$stmt->execute()) {
throw new Exception($stmt->error);
}
$stmt->close();
}
$conn->commit();
echo json_encode(['success' => true, 'message' => 'Order placed successfully']);
} catch (Exception $e) {
$conn->rollback();
echo json_encode(['success' => false, 'message' => 'Failed to place order', 'error' => $e->getMessage()]);
}
} else {
echo json_encode(['success' => false, 'message' => 'Invalid input data']);
}
$conn->close();
?>