Files
UAS_WEBGIS_CindyDyraIslamay…/01SPBU/SPBU/simpan.php
T
2026-06-10 19:20:46 +07:00

52 lines
1.5 KiB
PHP

<?php
header('Content-Type: application/json');
try {
require_once __DIR__ . '/../koneksi.php';
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
throw new Exception('Invalid request method');
}
$nama = isset($_POST['nama']) ? trim($_POST['nama']) : '';
$nomor = isset($_POST['nomor']) ? trim($_POST['nomor']) : '';
$kategori = isset($_POST['kategori']) ? trim($_POST['kategori']) : '';
$lat = isset($_POST['latitude']) ? floatval($_POST['latitude']) : null;
$lng = isset($_POST['longitude']) ? floatval($_POST['longitude']) : null;
if (empty($nama) || empty($nomor) || empty($kategori)) {
throw new Exception('Semua field harus diisi');
}
$sql = "INSERT INTO spbu (nama, nomor, kategori, latitude, longitude) VALUES (?, ?, ?, ?, ?)";
$stmt = $conn->prepare($sql);
if (!$stmt) {
throw new Exception('Prepare failed: ' . $conn->error);
}
$stmt->bind_param("sssdd", $nama, $nomor, $kategori, $lat, $lng);
if ($stmt->execute()) {
echo json_encode([
'status' => 'success',
'message' => 'Data berhasil disimpan',
'id' => $conn->insert_id
]);
} else {
throw new Exception('Execute failed: ' . $stmt->error);
}
$stmt->close();
} catch (Exception $e) {
echo json_encode([
'status' => 'error',
'message' => $e->getMessage()
]);
}
if (isset($conn)) {
$conn->close();
}
?>