From 805f152be1c0a09fd661bc35441d95cc837da2f0 Mon Sep 17 00:00:00 2001 From: Nama Kamu Date: Thu, 11 Jun 2026 15:02:32 +0700 Subject: [PATCH] Fix trailing whitespace and JSON parse errors on backend --- 04/ambil_data.php | 11 +++++++---- 04/hapus.php | 3 +-- 04/index.php | 8 ++++---- 04/koneksi.php | 1 - 04/simpan.php | 3 +-- 04/update.php | 3 +-- init-db.php | 6 +++--- 7 files changed, 17 insertions(+), 18 deletions(-) diff --git a/04/ambil_data.php b/04/ambil_data.php index ad04870..7adfbff 100644 --- a/04/ambil_data.php +++ b/04/ambil_data.php @@ -5,9 +5,12 @@ $data = []; $result = $conn->query("SELECT * FROM spbu"); -while ($row = $result->fetch_assoc()) { - $data[] = $row; +if ($result) { + while ($row = $result->fetch_assoc()) { + $data[] = $row; + } +} else { + // If table doesn't exist or other error, return empty array } -echo json_encode($data); -?> \ No newline at end of file +echo json_encode($data); \ No newline at end of file diff --git a/04/hapus.php b/04/hapus.php index 8608056..147dfde 100644 --- a/04/hapus.php +++ b/04/hapus.php @@ -10,6 +10,5 @@ $query = "DELETE FROM spbu WHERE id='$id'"; if ($conn->query($query)) { echo "success"; } else { - echo "error"; + echo "error: " . $conn->error; } -?> diff --git a/04/index.php b/04/index.php index 9e6348d..feadab9 100644 --- a/04/index.php +++ b/04/index.php @@ -266,7 +266,7 @@ lng:pos.lng }) .then(r=>r.text()) .then(res=>{ -if(res=="success"){ +if(res.trim()=="success"){ alert("Lokasi berhasil diupdate"); location.reload(); } @@ -348,7 +348,7 @@ body:fd }) .then(r=>r.text()) .then(res=>{ -if(res=="success"){ +if(res.trim()=="success"){ alert("Data berhasil disimpan"); location.reload(); }else{ @@ -422,7 +422,7 @@ lng:lng }) .then(r=>r.text()) .then(res=>{ -if(res=="success"){ +if(res.trim()=="success"){ alert("Data berhasil diupdate"); location.reload(); }else{ @@ -452,7 +452,7 @@ id:id }) .then(r=>r.text()) .then(res=>{ -if(res=="success"){ +if(res.trim()=="success"){ alert("Data berhasil dihapus"); location.reload(); } diff --git a/04/koneksi.php b/04/koneksi.php index 69d7e4b..3d03dd2 100644 --- a/04/koneksi.php +++ b/04/koneksi.php @@ -9,4 +9,3 @@ $conn = mysqli_connect($host, $user, $pass, $db); if (!$conn) { die("Koneksi database gagal: " . mysqli_connect_error()); } -?> diff --git a/04/simpan.php b/04/simpan.php index 5aa66b4..49bed42 100644 --- a/04/simpan.php +++ b/04/simpan.php @@ -15,6 +15,5 @@ $query = "INSERT INTO spbu (nama_spbu, no_wa, status, latitude, longitude) if ($conn->query($query)) { echo "success"; } else { - echo "error"; + echo "error: " . $conn->error; } -?> diff --git a/04/update.php b/04/update.php index f61df4f..1db56b7 100644 --- a/04/update.php +++ b/04/update.php @@ -21,6 +21,5 @@ $query = "UPDATE spbu SET if ($conn->query($query)) { echo "success"; } else { - echo "error"; + echo "error: " . $conn->error; } -?> diff --git a/init-db.php b/init-db.php index 118412a..5bc74f4 100644 --- a/init-db.php +++ b/init-db.php @@ -40,11 +40,11 @@ function importSql($conn, $file) { // Cek apakah tabel uas_06 sudah ada (menggunakan tabel role sebagai patokan) $res = $conn->query("SHOW TABLES LIKE 'role'"); -if ($res->num_rows == 0) { +if ($res && $res->num_rows == 0) { echo "Tabel utama (05) belum ada, menjalankan import...\n"; importSql($conn, __DIR__ . '/05/database_uas_06.sql'); } else { - echo "Tabel utama (05) sudah ada, skip import.\n"; + echo "Tabel utama (05) sudah ada atau query gagal, skip import.\n"; } // Cek tabel SPBU (04) @@ -52,7 +52,7 @@ $db04 = getenv('DB_NAME_04') ?: $db; $conn->query("CREATE DATABASE IF NOT EXISTS `$db04` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"); $conn->select_db($db04); $res = $conn->query("SHOW TABLES LIKE 'spbu'"); -if ($res->num_rows == 0) { +if ($res && $res->num_rows == 0) { echo "Tabel spbu (04) belum ada, menjalankan import...\n"; importSql($conn, __DIR__ . '/04/db_spbu.sql'); } else {