<?php
require 'koneksi.php';

$nik   = $_POST['nik'];
$nama  = $_POST['nama'];
$user  = $_POST['username'];
$pass  = $_POST['password'];
$telp  = $_POST['telp'];

// Buat prepared statement untuk keamanan
$sql = "INSERT INTO masyarakat (nik, nama, username, password, telp) VALUES (?, ?, ?, ?, ?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param("sssss", $nik, $nama, $user, $pass, $telp);

if ($stmt->execute()) {
    echo "<script>
        alert('Data Berhasil Disimpan, Silahkan Gunakan Untuk Login');
        window.location='index.php';
    </script>";
} else {
    echo "<script>
        alert('Gagal menyimpan data. Pastikan NIK belum terdaftar!');
        window.location='index.php';
    </script>";
}

$stmt->close();
$conn->close();
?>
