TKK_E32230273/edit_occupant.php

43 lines
1.4 KiB
PHP

<?php
include "koneksi.php";
header('Content-Type: application/json');
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
echo json_encode(["status" => "error", "message" => "Method tidak diizinkan"]);
exit;
}
$id = isset($_POST['id_occupant']) ? $_POST['id_occupant'] : '';
if (empty($id) || !is_numeric($id)) {
echo json_encode(["status" => "error", "message" => "ID tidak valid"]);
exit;
}
$name = isset($_POST['name']) ? trim($_POST['name']) : '';
$nik_nip = isset($_POST['nik_nip']) ? trim($_POST['nik_nip']) : '';
$no_hp = isset($_POST['no_hp']) ? trim($_POST['no_hp']) : '';
if (empty($name) || empty($no_hp)) {
echo json_encode(["status" => "error", "message" => "Nama dan Username wajib diisi"]);
exit;
}
// Cek no_hp unik (selain diri sendiri)
$cek = $pdo->prepare("SELECT COUNT(*) FROM occupant WHERE no_hp = ? AND id_occupant != ?");
$cek->execute([$no_hp, $id]);
if ($cek->fetchColumn() > 0) {
echo json_encode(["status" => "error", "message" => "Nomor Handphone sudah digunakan"]);
exit;
}
try {
$stmt = $pdo->prepare("UPDATE occupant SET name = ?, nik_nip = ?, no_hp = ? WHERE id_occupant = ?");
$stmt->execute([$name, $nik_nip, $no_hp, $id]);
echo json_encode(["status" => "success"]);
} catch (PDOException $e) {
echo json_encode(["status" => "error", "message" => "Gagal mengupdate: " . $e->getMessage()]);
}
?>