Files
UAS-SIG-d1041231006/parsil-tanah/save_jalan.php
T
2026-06-10 19:36:52 +07:00

39 lines
1.3 KiB
PHP

<?php
require 'db.php';
header('Content-Type: application/json; charset=utf-8');
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['status' => 'error', 'message' => 'Method not allowed.']);
exit;
}
$nama_jalan = $_POST['nama_jalan'] ?? null;
$status_jalan = $_POST['status_jalan'] ?? null;
$panjang_meter = $_POST['panjang_meter'] ?? null;
$koordinat = $_POST['koordinat'] ?? null;
if ($nama_jalan === null || $status_jalan === null || $panjang_meter === null || $koordinat === null) {
http_response_code(400);
echo json_encode(['status' => 'error', 'message' => 'Missing required fields.']);
exit;
}
$decoded = json_decode($koordinat, true);
if ($decoded === null) {
http_response_code(400);
echo json_encode(['status' => 'error', 'message' => 'Invalid koordinat JSON.']);
exit;
}
$sql = "INSERT INTO tb_jalan (nama_jalan, status_jalan, panjang_meter, koordinat) VALUES (?, ?, ?, ?)";
$stmt = $pdo->prepare($sql);
if ($stmt->execute([$nama_jalan, $status_jalan, (float)$panjang_meter, $koordinat])) {
echo json_encode(['status' => 'success', 'message' => 'Jalan saved.']);
} else {
http_response_code(500);
echo json_encode(['status' => 'error', 'message' => 'Failed to save jalan.']);
}