-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcart-quantity-update.php
59 lines (51 loc) · 1.89 KB
/
cart-quantity-update.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
<?php
function productAddToCart() {
// Retrieve inputs and validate them
$cartId = isset($_POST['cart_id']) ? intval($_POST['cart_id']) : 0;
$productQuantity = isset($_POST['productQuantity']) ? intval($_POST['productQuantity']) : 0;
$productPrice = isset($_POST['productPrice']) ? intval($_POST['productPrice']) : 0;
$totalPrice = $productPrice * $productQuantity;
// Check if inputs are valid
if ($cartId <= 0 || $productQuantity < 0) {
echo json_encode([
'message' => 'Invalid input',
'success' => false,
]);
return;
}
include_once 'databaseConnection.php';
try {
// Initialize database connection
$db = new \DB\DBConnection(); // Use namespace
$mysqli = $db->dbConnect();
// Prepare the SQL query
$stmt = $mysqli->prepare("UPDATE carts SET quantity = ?,total_price = ? WHERE id = ?");
if (!$stmt) {
throw new Exception('Failed to prepare statement: ' . $mysqli->error);
}
// Bind parameters and execute
$stmt->bind_param("idi", $productQuantity, $totalPrice, $cartId);
$saveProduct = $stmt->execute();
// Check if the update was successful
if ($saveProduct && $stmt->affected_rows > 0) {
echo json_encode([
'message' => 'Quantity updated successfully',
'success' => true,
]);
} else {
echo json_encode([
'message' => 'No rows updated. Check if the cart ID exists.',
'success' => false,
]);
}
// Close the statement
$stmt->close();
} catch (Exception $e) {
// Handle exceptions and send error response
echo json_encode([
'message' => 'Error: ' . $e->getMessage(),
'success' => false,
]);
}
}
productAddToCart();