40 lines
1.3 KiB
PHP
40 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;
|
|
}
|
|
|
|
$id = $_POST['id'] ?? null;
|
|
$nama_jalan = $_POST['nama_jalan'] ?? null;
|
|
$status_jalan = $_POST['status_jalan'] ?? null;
|
|
$panjang_meter = $_POST['panjang_meter'] ?? null;
|
|
$koordinat = $_POST['koordinat'] ?? null;
|
|
|
|
if ($id === null || $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 = "UPDATE tb_jalan SET nama_jalan = ?, status_jalan = ?, panjang_meter = ?, koordinat = ? WHERE id = ?";
|
|
$stmt = $pdo->prepare($sql);
|
|
|
|
if ($stmt->execute([$nama_jalan, $status_jalan, (float)$panjang_meter, $koordinat, (int)$id])) {
|
|
echo json_encode(['status' => 'success', 'message' => 'Jalan updated.']);
|
|
} else {
|
|
http_response_code(500);
|
|
echo json_encode(['status' => 'error', 'message' => 'Failed to update jalan.']);
|
|
}
|