<?php 
	// koneksi database
	include "koneksi.php";

	$query = mysqli_query($koneksi, "SELECT max(NOREG) as idTerbesar FROM daftar");
	$data = mysqli_fetch_array($query);
	$Regkode = $data['idTerbesar'];
 
	$urutan = (int) substr($Regkode, 5, 4);
	$urutan++;
 
	$huruf = "R";
	$tahun = Date('y');
	$bulan = Date('m');
	$Regkode = $huruf . $tahun . $bulan . sprintf("%04s", $urutan);
	// $Regkode = $tahun . $bulan . sprintf("%04s", $urutan);

	// menangkap data yang di kirim dari form
	if (isset($_POST['daftar'])) {
		$NOREG		= $Regkode;
		$NAME		= $_POST['fullname'];
		$NOID		= $_POST['NO_ID'];
		$NOKK		= $_POST['NO_KK'];
		$T_LAHIR	= $_POST['TEMPAT_LAHIR'];
		$TGL_LAHIR	= $_POST['TANGGAL_LAHIR'];
		$J_KELAMIN	= $_POST['JENIS_KELAMIN'];
		$GOL_DARAH	= $_POST['GOL_DARAH'];
		$ALMT		= $_POST['ALAMAT'];
		$RT			= $_POST['RT'];
		$RW			= $_POST['RW'];
		$KEL		= $_POST['KEL'];
		$KEC		= $_POST['KEC'];
		$KOTA		= $_POST['KOTA'];
		$PROV		= $_POST['PROVINSI'];
		$KODEPOS 	= $_POST['KODEPOS'];
		$STS 		= $_POST['STATUS_PERNIKAHAN'];
		$AGAMA 		= $_POST['AGAMA'];
		$JLH_ANAK 	= $_POST['JUMLAH_ANAK'];
		$NOHP 		= $_POST['HP'];
		$NO_REK 	= $_POST['NO_REKENING'];
		$NAMA_REK 	= $_POST['NAMA_PEMILIK_REKENING'];
		$NAMABANK 	= $_POST['NAMA_BANK'];
		$NAMAIBU 	= $_POST['NAMA_IBU_KANDUNG'];
		$MAIL 		= $_POST['EMAIL'];
	    $TGLREGIST 	= date('Y-m-d H:i:s');
		
			$regist = "select * from daftar where NO_ID = $NOID";
			$result = mysqli_query($koneksi, $regist);

			if(mysqli_num_rows($result) > 0){
				$query = mysqli_query($koneksi, "SELECT NO_ID as KTP, NAMA FROM daftar where NO_ID = $NOID");
				$data = mysqli_fetch_array($query);
				$NOID = $data['KTP'];
				$NAME = $data['NAMA'];
				
				echo '<script>alert("NO KTP : '.$NOID.' \nSudah Digunakan \n'.$NAME.'.!");history.go(-1);</script>';

			}else{

				$q=mysqli_query($koneksi,"insert into daftar values ('$NOREG','$NAME','$NOID','$NOKK','$T_LAHIR','$TGL_LAHIR','$J_KELAMIN','$GOL_DARAH','$ALMT','$RT','$RW','$KEL','$KEC','$KOTA','$PROV','$KODEPOS','$STS','$AGAMA','$JLH_ANAK','$NOHP','$NO_REK','$NAMA_REK','$NAMABANK','$NAMAIBU','$MAIL','PELAMAR','$TGLREGIST')")or die(mysqli_error($koneksi));

				if($q){
					echo '<div class="alert alert-success alert-dismissible fade in" role="alert">
					<button type="button" class="close" data-dismiss="alert" aria-label="Close"> <span aria-hidden="true">&times;</span> </button>
					<strong> Sukses..!</strong> Data Berhasil Tersimpan <strong> .!!</strong>
					</div>';
					echo '<meta http-equiv="refresh" content="3;url=index.html">';
				}else{
					echo '<script>alert("'.$nikuser.' Sudah Digunakan '.$namauser.'.");history.go(-1);</script>';
				}
			}
	}
	
		// menginput data ke database
		// mysqli_query($koneksi,"INSERT INTO MASUK VALUES ('$uname','$fname','$pw','$no_hp','$mail','OS')")or die(mysqli_error($koneksi));
			
		
		// $inputsql = "INSERT INTO masuk VALUES ('$uname','$fname','$pw','$no_hp','$mail','OS')";

		
		
	// mengalihkan halaman kembali ke index.php
	// header('location:masuk.php');

?>