-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdateProduct.php
45 lines (35 loc) · 1.42 KB
/
updateProduct.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
<?php
// updateProduct.php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization");
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(200);
exit();
}
header('Content-Type: application/json');
include 'Conx.php'; // Ensure the correct path to your Conx.php file
// Get input data
$data = json_decode(file_get_contents('php://input'), true);
if (isset($data['id'], $data['name'], $data['description'], $data['price'], $data['quantity'])) {
$id = (int) $data['id'];
$name = $data['name'];
$description = $data['description'];
$price = (float) $data['price'];
$quantity = (int) $data['quantity'];
// Prepare and execute the update query
$query = "UPDATE products SET name = ?, description = ?, price = ?, quantity = ? WHERE id = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param('ssdii', $name, $description, $price, $quantity, $id);
$stmt->execute();
if ($stmt->affected_rows > 0) {
echo json_encode(['success' => true, 'message' => 'Product updated successfully.']);
} else {
echo json_encode(['success' => false, 'message' => 'No changes made or product not found.']);
}
$stmt->close();
} else {
echo json_encode(['success' => false, 'message' => 'Invalid input']);
}
$conn->close();
?>