-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetProduct.php
39 lines (31 loc) · 1.13 KB
/
getProduct.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
<?php
// getProduct.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();
}
include 'Conx.php'; // Ensure the correct path to your Conx.php file
// Check if the 'id' parameter exists in the query string
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
$id = (int) $_GET['id']; // Cast the value to an integer
// Prepare and execute the SQL query
$query = "SELECT * FROM products WHERE id = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param('i', $id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$product = $result->fetch_assoc();
echo json_encode(['success' => true, 'product' => $product]);
} else {
echo json_encode(['success' => false, 'message' => 'Product not found.']);
}
$stmt->close();
} else {
echo json_encode(['success' => false, 'message' => 'Invalid input']);
}
$conn->close();
?>