Initial commit
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
header('Content-Type: application/json');
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') { echo json_encode(['status'=>'error']); exit(); }
|
||||
require_once 'db_config.php';
|
||||
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
$id = intval($data['id'] ?? 0);
|
||||
$nama = trim($data['nama_lengkap'] ?? '');
|
||||
$role = trim($data['role'] ?? '');
|
||||
$pass = $data['password'] ?? '';
|
||||
|
||||
if (!$id || !$nama || !in_array($role, ['admin','operator'])) {
|
||||
echo json_encode(['status'=>'error','message'=>'Data tidak valid']);
|
||||
exit();
|
||||
}
|
||||
|
||||
if ($pass) {
|
||||
if (strlen($pass) < 6) { echo json_encode(['status'=>'error','message'=>'Password minimal 6 karakter']); exit(); }
|
||||
$hashed = password_hash($pass, PASSWORD_DEFAULT);
|
||||
$stmt = $conn->prepare("UPDATE users SET nama_lengkap=?, role=?, password=? WHERE id=?");
|
||||
$stmt->bind_param('sssi', $nama, $role, $hashed, $id);
|
||||
} else {
|
||||
$stmt = $conn->prepare("UPDATE users SET nama_lengkap=?, role=? WHERE id=?");
|
||||
$stmt->bind_param('ssi', $nama, $role, $id);
|
||||
}
|
||||
|
||||
echo $stmt->execute()
|
||||
? json_encode(['status'=>'success','message'=>'Akun berhasil diperbarui'])
|
||||
: json_encode(['status'=>'error','message'=>'Gagal memperbarui akun']);
|
||||
|
||||
$stmt->close();
|
||||
$conn->close();
|
||||
?>
|
||||
Reference in New Issue
Block a user