menambahkan fitur edit data pengguna dan memperbarui landing page
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
require_once '../../auth.php';
|
||||
requireRole('admin');
|
||||
|
||||
header('Content-Type: application/json');
|
||||
$conn = new mysqli("localhost", "root", "", "webgis");
|
||||
$data = json_decode(file_get_contents("php://input"), true);
|
||||
|
||||
if (!$data || !isset($data['id'])) {
|
||||
echo json_encode(["status" => "error", "message" => "Data tidak lengkap"]);
|
||||
exit;
|
||||
}
|
||||
|
||||
$id = (int) $data['id'];
|
||||
$nama = trim($data['nama']);
|
||||
$username = trim($data['username']);
|
||||
$role = $data['role'];
|
||||
|
||||
if (!$nama || !$username || !in_array($role, ['admin', 'operator'])) {
|
||||
echo json_encode(["status" => "error", "message" => "Data tidak valid"]);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Cek username sudah dipakai user lain
|
||||
$cek = $conn->prepare("SELECT id FROM users WHERE username = ? AND id != ?");
|
||||
$cek->bind_param("si", $username, $id);
|
||||
$cek->execute();
|
||||
$cek->store_result();
|
||||
if ($cek->num_rows > 0) {
|
||||
echo json_encode(["status" => "error", "message" => "Username sudah digunakan user lain"]);
|
||||
$cek->close();
|
||||
$conn->close();
|
||||
exit;
|
||||
}
|
||||
$cek->close();
|
||||
|
||||
// Update dengan atau tanpa password baru
|
||||
if (!empty($data['password'])) {
|
||||
$hash = password_hash($data['password'], PASSWORD_DEFAULT);
|
||||
$stmt = $conn->prepare("UPDATE users SET nama=?, username=?, password=?, role=? WHERE id=?");
|
||||
$stmt->bind_param("ssssi", $nama, $username, $hash, $role, $id);
|
||||
} else {
|
||||
$stmt = $conn->prepare("UPDATE users SET nama=?, username=?, role=? WHERE id=?");
|
||||
$stmt->bind_param("sssi", $nama, $username, $role, $id);
|
||||
}
|
||||
|
||||
if ($stmt->execute()) {
|
||||
echo json_encode(["status" => "success"]);
|
||||
} else {
|
||||
echo json_encode(["status" => "error", "message" => $stmt->error]);
|
||||
}
|
||||
|
||||
$stmt->close();
|
||||
$conn->close();
|
||||
?>
|
||||
Reference in New Issue
Block a user