35 lines
854 B
PHP
35 lines
854 B
PHP
<?php
|
|
require_once __DIR__ . '/../db/config.php';
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
|
|
http_response_code(405);
|
|
echo json_encode(['error' => 'Method Not Allowed']);
|
|
exit;
|
|
}
|
|
|
|
$id = $_GET['id'] ?? null;
|
|
|
|
if (!$id) {
|
|
http_response_code(400);
|
|
echo json_encode(['error' => 'Missing required parameter: id']);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("DELETE FROM products WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
|
|
if ($stmt->rowCount() > 0) {
|
|
echo json_encode(['message' => 'Product deleted successfully']);
|
|
} else {
|
|
http_response_code(404);
|
|
echo json_encode(['error' => 'Product not found']);
|
|
}
|
|
} catch (PDOException $e) {
|
|
http_response_code(500);
|
|
echo json_encode(['error' => 'Database error: ' . $e->getMessage()]);
|
|
}
|