-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_item.php
More file actions
43 lines (34 loc) · 1.24 KB
/
update_item.php
File metadata and controls
43 lines (34 loc) · 1.24 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
<?php
// update_item.php
header('Content-Type: application/json');
// Database connection parameters
$servername = "localhost";
$username = "item_db"; // Replace with your MySQL username
$password = "haxx0r"; // Replace with your MySQL password
$dbname = "shop_db"; // Ensure this matches your database name
$response = array('success' => false, 'message' => '');
try {
// Create a new PDO connection
$conn = new PDO("mysql:host=$servername;dbname=$dbname;charset=utf8mb4", $username, $password);
// Get and sanitize POST data
$id = $_POST['id'] ?? '';
$name = $_POST['name'] ?? '';
$description = $_POST['description'] ?? '';
$price = $_POST['price'] ?? '';
if (empty($id) || empty($name) || empty($price)) {
throw new Exception('ID, name, and price are required.');
}
// Update the item
$stmt = $conn->prepare("UPDATE items SET name = :name, description = :description, price = :price WHERE id = :id");
$stmt->execute([
':id' => $id,
':name' => $name,
':description' => $description,
':price' => $price
]);
$response['success'] = true;
} catch (Exception $e) {
$response['message'] = $e->getMessage();
}
echo json_encode($response);
?>