Files
WebGIS-Tugas-Setiap-Pertemuan/SPBU/api/update_spbu.php
T

49 lines
1.7 KiB
PHP

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: PUT");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
session_start();
require_once '../config/database.php';
if (!isset($_SESSION['is_admin']) || $_SESSION['is_admin'] !== true) {
http_response_code(401);
echo json_encode(array("message" => "Unauthorized"));
exit();
}
$database = new Database();
$db = $database->getConnection();
$data = json_decode(file_get_contents("php://input"));
if (!empty($data->id)) {
try {
$query = "UPDATE spbu SET nama = :nama, no_spbu = :no_spbu, status = :status, latitude = :latitude, longitude = :longitude WHERE id = :id";
$stmt = $db->prepare($query);
$stmt->bindParam(":nama", $data->nama);
$stmt->bindParam(":no_spbu", $data->no_spbu);
$stmt->bindParam(":status", $data->status);
$stmt->bindParam(":latitude", $data->latitude);
$stmt->bindParam(":longitude", $data->longitude);
$stmt->bindParam(":id", $data->id);
if ($stmt->execute()) {
http_response_code(200);
echo json_encode(array("message" => "SPBU berhasil diupdate"));
} else {
http_response_code(503);
echo json_encode(array("message" => "Gagal mengupdate SPBU"));
}
} catch(PDOException $e) {
http_response_code(500);
echo json_encode(array("message" => "Error: " . $e->getMessage()));
}
} else {
http_response_code(400);
echo json_encode(array("message" => "ID tidak ditemukan"));
}
?>