Files
D1041231091-WebGIS-All/02/hapus.php
T

61 lines
1.5 KiB
PHP

<?php
/**
* hapus.php
* Menangani hapus data SPBU, polyline, dan polygon.
* Gunakan parameter 'tipe' untuk membedakan jenis data.
*/
header('Content-Type: application/json');
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['status' => 'error', 'message' => 'Method tidak diizinkan.']);
exit;
}
require_once 'koneksi.php';
$tipe = isset($_POST['tipe']) ? trim($_POST['tipe']) : '';
$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
if ($id <= 0) {
echo json_encode(['status' => 'error', 'message' => 'ID tidak valid.']);
exit;
}
// Mapping tipe ke nama tabel
$tabel_map = [
'spbu' => 'spbu',
'polyline' => 'polyline',
'polygon' => 'polygon',
];
if (!array_key_exists($tipe, $tabel_map)) {
echo json_encode(['status' => 'error', 'message' => 'Tipe tidak dikenali.']);
exit;
}
$tabel = $tabel_map[$tipe];
// Prepared statement hapus berdasarkan tipe dan id
$stmt = $koneksi->prepare("DELETE FROM `{$tabel}` WHERE id = ?");
$stmt->bind_param('i', $id);
if ($stmt->execute()) {
if ($stmt->affected_rows > 0) {
echo json_encode([
'status' => 'success',
'message' => ucfirst($tipe) . ' berhasil dihapus.',
'id' => $id,
'tipe' => $tipe
]);
} else {
echo json_encode(['status' => 'error', 'message' => 'Data tidak ditemukan.']);
}
} else {
echo json_encode(['status' => 'error', 'message' => 'Gagal menghapus: ' . $stmt->error]);
}
$stmt->close();
$koneksi->close();