Add auto-import DB script and update connection fallback

This commit is contained in:
Nama Kamu
2026-06-11 14:30:22 +07:00
commit 2aa30691f1
44 changed files with 8042 additions and 0 deletions
+12
View File
@@ -0,0 +1,12 @@
<?php
include 'koneksi.php';
$result = $koneksi->query("SELECT * FROM spbu");
$data = [];
while($row = $result->fetch_assoc()){
$data[] = $row;
}
echo json_encode($data);
?>
+69
View File
@@ -0,0 +1,69 @@
-- phpMyAdmin SQL Dump
-- version 5.2.1
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Waktu pembuatan: 07 Jun 2026 pada 10.53
-- Versi server: 10.4.32-MariaDB
-- Versi PHP: 8.2.12
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `db_spbu`
--
-- --------------------------------------------------------
--
-- Struktur dari tabel `spbu`
--
CREATE TABLE `spbu` (
`id` int(11) NOT NULL,
`nama_spbu` varchar(255) DEFAULT NULL,
`lat` double DEFAULT NULL,
`lng` double DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
--
-- Dumping data untuk tabel `spbu`
--
INSERT INTO `spbu` (`id`, `nama_spbu`, `lat`, `lng`) VALUES
(1, 'spbu kobar', -0.02197187355969219, 109.31207858442205),
(3, 'spbu paris', 0.007368924759898348, 109.37010012983221),
(4, 'spbu tanray', 0.016651739943910275, 109.29988329640928);
--
-- Indexes for dumped tables
--
--
-- Indeks untuk tabel `spbu`
--
ALTER TABLE `spbu`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT untuk tabel yang dibuang
--
--
-- AUTO_INCREMENT untuk tabel `spbu`
--
ALTER TABLE `spbu`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
+10
View File
@@ -0,0 +1,10 @@
<?php
include 'koneksi.php';
$id = $_POST['id'];
$stmt = $koneksi->prepare("DELETE FROM spbu WHERE id=?");
$stmt->bind_param("i", $id);
echo $stmt->execute() ? "success" : "error";
?>
+203
View File
@@ -0,0 +1,203 @@
<!DOCTYPE html>
<html>
<head>
<title>Peta SPBU</title>
<link rel="stylesheet" href="https://unpkg.com/leaflet/dist/leaflet.css"/>
<script src="https://unpkg.com/leaflet/dist/leaflet.js"></script>
<style>
#map { height: 100vh; }
</style>
</head>
<body>
<div id="map"></div>
<script>
const map = L.map('map').setView([-0.02, 109.34], 13);
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19
}).addTo(map);
let layer = L.layerGroup().addTo(map);
let data = [];
// =====================
// NOTIFIKASI POPUP
// =====================
function showNotif(text) {
let popup = L.popup()
.setLatLng(map.getCenter())
.setContent(`<div style="padding:6px;">${text}</div>`)
.openOn(map);
setTimeout(() => {
map.closePopup(popup);
}, 1500);
}
// =====================
// LOAD DATA
// =====================
function load() {
fetch('ambil_data.php')
.then(res => res.json())
.then(res => {
data = res;
render();
});
}
// =====================
// RENDER MARKER
// =====================
function render() {
layer.clearLayers();
data.forEach(d => {
let marker = L.marker([d.lat, d.lng], {
draggable: true
}).addTo(layer);
marker.bindPopup(`
<b>${d.nama_spbu}</b><br><br>
<button onclick="edit(${d.id}, '${d.nama_spbu}', ${d.lat}, ${d.lng})">
Edit
</button>
<button onclick="hapus(${d.id})">
Hapus
</button>
`);
// =====================
// DRAG UPDATE
// =====================
marker.on('dragend', function(e){
let pos = e.target.getLatLng();
fetch('update.php', {
method: 'POST',
body: new URLSearchParams({
id: d.id,
nama: d.nama_spbu,
lat: pos.lat,
lng: pos.lng
})
})
.then(res => res.text())
.then(res => {
if(res.trim() === "success"){
showNotif("📍 Lokasi berhasil diupdate");
load();
} else {
showNotif("❌ Gagal update");
}
});
});
});
}
load();
// =====================
// TAMBAH DATA (klik peta)
// =====================
map.on('click', function(e){
let lat = e.latlng.lat;
let lng = e.latlng.lng;
let popup = L.popup()
.setLatLng(e.latlng)
.setContent(`
<form id="form">
<input name="nama" placeholder="Nama SPBU" required>
<button type="submit">Simpan</button>
</form>
`)
.openOn(map);
setTimeout(() => {
document.getElementById("form").onsubmit = function(ev){
ev.preventDefault();
let fd = new FormData(this);
fd.append("lat", lat);
fd.append("lng", lng);
fetch('simpan.php', {
method: 'POST',
body: fd
})
.then(res => res.text())
.then(res => {
if(res.trim() === "success"){
showNotif("✅ SPBU berhasil ditambahkan");
load();
map.closePopup();
} else {
showNotif("❌ Gagal menyimpan");
}
});
}
}, 100);
});
// =====================
// EDIT DATA
// =====================
function edit(id, nama, lat, lng){
let newNama = prompt("Nama SPBU:", nama);
if(newNama){
fetch('update.php', {
method: 'POST',
body: new URLSearchParams({
id,
nama: newNama,
lat,
lng
})
})
.then(res => res.text())
.then(res => {
if(res.trim() === "success"){
showNotif("✏️ Data berhasil diupdate");
load();
} else {
showNotif("❌ Gagal update");
}
});
}
}
// =====================
// HAPUS DATA
// =====================
function hapus(id){
if(confirm("Yakin mau hapus data ini?")){
fetch('hapus.php', {
method: 'POST',
body: new URLSearchParams({id})
})
.then(res => res.text())
.then(res => {
if(res.trim() === "success"){
showNotif("🗑️ Data berhasil dihapus");
load();
} else {
showNotif("❌ Gagal hapus");
}
});
}
}
</script>
</body>
</html>
+14
View File
@@ -0,0 +1,14 @@
<?php
$host = getenv('DB_HOST') ?: 'localhost';
$user = getenv('DB_USER') ?: 'root';
$pass = getenv('DB_PASSWORD') ?: '';
$db = getenv('DB_NAME') ?: 'db_spbu';
$conn = mysqli_connect($host, $user, $pass, $db);
if (!$conn) {
die("Koneksi database gagal: " . mysqli_connect_error());
}
$koneksi = $conn;
?>
+12
View File
@@ -0,0 +1,12 @@
<?php
include 'koneksi.php';
$nama = $_POST['nama'];
$lat = $_POST['lat'];
$lng = $_POST['lng'];
$stmt = $koneksi->prepare("INSERT INTO spbu (nama_spbu, lat, lng) VALUES (?, ?, ?)");
$stmt->bind_param("sdd", $nama, $lat, $lng);
echo $stmt->execute() ? "success" : "error";
?>
+13
View File
@@ -0,0 +1,13 @@
<?php
include 'koneksi.php';
$id = $_POST['id'];
$nama = $_POST['nama'];
$lat = $_POST['lat'];
$lng = $_POST['lng'];
$stmt = $koneksi->prepare("UPDATE spbu SET nama_spbu=?, lat=?, lng=? WHERE id=?");
$stmt->bind_param("sddi", $nama, $lat, $lng, $id);
echo $stmt->execute() ? "success" : "error";
?>