Initial Commit: Tugas Semua Pertemuan
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
include 'koneksi.php';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$id = intval($_POST['id']);
|
||||
$deskripsi = mysqli_real_escape_string($conn, $_POST['deskripsi']);
|
||||
$fotoLama = mysqli_real_escape_string($conn, $_POST['foto_lama']);
|
||||
|
||||
// Jika ada upload foto baru
|
||||
if (isset($_FILES['foto']) && $_FILES['foto']['error'] === UPLOAD_ERR_OK) {
|
||||
$targetDir = "uploads/";
|
||||
$fileName = time() . '_' . basename($_FILES["foto"]["name"]);
|
||||
$targetFilePath = $targetDir . $fileName;
|
||||
$fileType = strtolower(pathinfo($targetFilePath, PATHINFO_EXTENSION));
|
||||
$allowedTypes = array('jpg', 'jpeg', 'png', 'gif');
|
||||
|
||||
if (in_array($fileType, $allowedTypes)) {
|
||||
if (move_uploaded_file($_FILES["foto"]["tmp_name"], $targetFilePath)) {
|
||||
// Hapus foto lama jika ada
|
||||
if (!empty($fotoLama) && file_exists($targetDir . $fotoLama)) {
|
||||
unlink($targetDir . $fotoLama);
|
||||
}
|
||||
$query = "UPDATE jalan_rusak SET deskripsi='$deskripsi', foto='$fileName' WHERE id=$id";
|
||||
} else {
|
||||
echo "upload_failed";
|
||||
exit;
|
||||
}
|
||||
} else {
|
||||
echo "invalid_format";
|
||||
exit;
|
||||
}
|
||||
} else {
|
||||
// Update tanpa ganti foto
|
||||
$query = "UPDATE jalan_rusak SET deskripsi='$deskripsi' WHERE id=$id";
|
||||
}
|
||||
|
||||
// Juga bisa update posisi jika dikirim dari drag marker
|
||||
if (isset($_POST['lat']) && isset($_POST['lng'])) {
|
||||
$lat = floatval($_POST['lat']);
|
||||
$lng = floatval($_POST['lng']);
|
||||
$query = "UPDATE jalan_rusak SET deskripsi='$deskripsi', latitude=$lat, longitude=$lng" . (isset($fileName) ? ", foto='$fileName'" : "") . " WHERE id=$id";
|
||||
}
|
||||
|
||||
if (mysqli_query($conn, $query)) {
|
||||
echo "success";
|
||||
} else {
|
||||
echo "db_error: " . mysqli_error($conn);
|
||||
}
|
||||
} else {
|
||||
echo "invalid_request";
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user