48 lines
1.5 KiB
PHP
48 lines
1.5 KiB
PHP
<?php
|
|
// php/create_kemiskinan.php
|
|
include 'koneksi.php';
|
|
|
|
$namaKepalaKeluarga = trim($_POST['nama_kepala_keluarga'] ?? '');
|
|
$jumlahKkRaw = $_POST['jumlah_kk'] ?? null;
|
|
$jumlahKk = filter_var($jumlahKkRaw, FILTER_VALIDATE_INT);
|
|
$alamat = trim($_POST['alamat'] ?? '');
|
|
$statusKeluarga = $_POST['status_keluarga'] ?? 'aktif';
|
|
$latitudeRaw = $_POST['lat'] ?? null;
|
|
$longitudeRaw = $_POST['lng'] ?? null;
|
|
$latitude = is_numeric($latitudeRaw) ? (float) $latitudeRaw : null;
|
|
$longitude = is_numeric($longitudeRaw) ? (float) $longitudeRaw : null;
|
|
|
|
$validStatus = ['aktif', 'meninggal', 'pindah', 'sudah_mampu'];
|
|
if (!in_array($statusKeluarga, $validStatus)) { $statusKeluarga = 'aktif'; }
|
|
|
|
if ($namaKepalaKeluarga === '' || $alamat === '' || $jumlahKk === false || $jumlahKk === null || $jumlahKk < 1 || $latitude === null || $longitude === null) {
|
|
http_response_code(400);
|
|
exit('error');
|
|
}
|
|
|
|
$statement = null;
|
|
|
|
try {
|
|
$statement = mysqli_prepare(
|
|
$conn,
|
|
"INSERT INTO kemiskinan (nama_kepala_keluarga, jumlah_kk, alamat, latitude, longitude, status_keluarga) VALUES (?, ?, ?, ?, ?, ?)"
|
|
);
|
|
|
|
mysqli_stmt_bind_param($statement, "sisdds", $namaKepalaKeluarga, $jumlahKk, $alamat, $latitude, $longitude, $statusKeluarga);
|
|
|
|
if (mysqli_stmt_execute($statement)) {
|
|
echo 'success';
|
|
} else {
|
|
http_response_code(500);
|
|
echo 'error';
|
|
}
|
|
} catch (mysqli_sql_exception $exception) {
|
|
http_response_code(500);
|
|
echo 'error: ' . $exception->getMessage();
|
|
}
|
|
|
|
if ($statement) {
|
|
mysqli_stmt_close($statement);
|
|
}
|
|
?>
|