-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert.php
97 lines (82 loc) · 3.16 KB
/
insert.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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
include 'connect.php';
header('Content-Type: application/json');
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
if (!$pdo) {
echo json_encode([
"status" => "error",
"message" => "Database connection failed"
]);
exit;
}
$data = json_decode(file_get_contents('php://input'), true);
if ($data) {
$firstName = trim($data['firstName']);
$lastName = trim($data['lastName']);
$contactNumber = trim($data['contactNumber']);
$email = trim($data['email']);
$city = trim($data['city']);
$state = trim($data['state']);
$paymentMethod = trim($data['paymentMethod']);
$grandTotal = floatval($data['grandTotal']);
$products = $data['products'];
try {
// Insert customer data
$customerQuery = "INSERT INTO customers (first_name, last_name, contact_number, email, city, state, payment_method)
VALUES (:firstName, :lastName, :contactNumber, :email, :city, :state, :paymentMethod)";
$stmt = $pdo->prepare($customerQuery);
$stmt->execute([
':firstName' => $firstName,
':lastName' => $lastName,
':contactNumber' => $contactNumber,
':email' => $email,
':city' => $city,
':state' => $state,
':paymentMethod' => $paymentMethod,
]);
// Get the last inserted ID
$orderID = $pdo->lastInsertId();
// Insert order summary
$orderQuery = "INSERT INTO orders_summary (grand_total) VALUES (:grandTotal)";
$stmt = $pdo->prepare($orderQuery);
$stmt->execute([':grandTotal' => $grandTotal]);
// Get the last inserted order ID
$orderID = $pdo->lastInsertId();
// Insert order details
foreach ($products as $product) {
$productName = trim($product['productName']);
$model = trim($product['model']);
$quantity = intval($product['quantity']); // Ensure quantity is an integer
$totalPrice = floatval($product['totalPrice']); // Ensure price is a float
$productQuery = "INSERT INTO order_details (order_id, product_name, model, quantity, total_price)
VALUES (:orderID, :productName, :model, :quantity, :totalPrice)";
$stmt = $pdo->prepare($productQuery);
$stmt->execute([
':orderID' => $orderID,
':productName' => $productName,
':model' => $model,
':quantity' => $quantity,
':totalPrice' => $totalPrice
]);
}
echo json_encode([
"status" => "success",
"message" => "Order details received and inserted successfully"
]);
} catch (PDOException $e) {
echo json_encode([
"status" => "error",
"message" => "Database error: " . $e->getMessage()
]);
}
} else {
echo json_encode([
"status" => "error",
"message" => "No data received"
]);
}
// Close the connection (optional for PDO)
$pdo = null;
?>