50 lines
2.0 KiB
PHP
50 lines
2.0 KiB
PHP
<?php
|
|
// ── DELETE SPBU ───────────────────────────────────────────────────────────────
|
|
require_once __DIR__ . '/config.php';
|
|
|
|
header('Content-Type: application/json');
|
|
header('Access-Control-Allow-Origin: *');
|
|
header('Access-Control-Allow-Methods: DELETE, POST, OPTIONS');
|
|
header('Access-Control-Allow-Headers: Content-Type');
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
|
http_response_code(200);
|
|
exit();
|
|
}
|
|
|
|
// Terima DELETE atau POST dengan _method=DELETE
|
|
$method = $_SERVER['REQUEST_METHOD'];
|
|
$body = file_get_contents('php://input');
|
|
$data = json_decode($body, true);
|
|
|
|
if ($method !== 'DELETE' && !($method === 'POST' && isset($data['_method']) && $data['_method'] === 'DELETE')) {
|
|
http_response_code(405);
|
|
echo json_encode(['success' => false, 'message' => 'Method not allowed']);
|
|
exit();
|
|
}
|
|
|
|
// ── VALIDASI ID ───────────────────────────────────────────────────────────────
|
|
$id = intval($data['id'] ?? 0);
|
|
if ($id <= 0) {
|
|
http_response_code(400);
|
|
echo json_encode(['success' => false, 'message' => 'ID tidak valid']);
|
|
exit();
|
|
}
|
|
|
|
// ── HAPUS DATA ────────────────────────────────────────────────────────────────
|
|
try {
|
|
$stmt = $pdo->prepare("DELETE FROM spbu WHERE id = :id");
|
|
$stmt->execute([':id' => $id]);
|
|
|
|
if ($stmt->rowCount() === 0) {
|
|
http_response_code(404);
|
|
echo json_encode(['success' => false, 'message' => 'Data tidak ditemukan']);
|
|
} else {
|
|
echo json_encode(['success' => true, 'message' => 'Data SPBU berhasil dihapus']);
|
|
}
|
|
|
|
} catch (PDOException $e) {
|
|
http_response_code(500);
|
|
echo json_encode(['success' => false, 'message' => 'Gagal menghapus data: ' . $e->getMessage()]);
|
|
}
|