<?php
	require_once("koneksi.php");
	$nama=mysqli_real_escape_string($conn,$_POST["username1"]);
	$password=mysqli_real_escape_string($conn,$_POST["password1"]);
	$jenisuser=mysqli_real_escape_string($conn,$_POST["jenisuser1"]);
	$alamat=mysqli_real_escape_string($conn,$_POST["alamat"]);
	$namabengkel=mysqli_real_escape_string($conn,$_POST["namabengkel"]);
	/*
	echo "masuk" . $nama, $password, $jenisuser;
	$daftar = "INSERT INTO user (username,password,level) values ('$nama','$password','$jenisuser')";
	$res = mysqli_query($conn, $daftar);
	echo "q : " . $daftar;*/
	//header("location:login.php");
	
	if(empty($password))
	{
		header("location:regis.php?p=1");
	}
	else if(empty($nama))
	{		
		header("location:regis.php?p=1");
	}
	else
	{
	  $q="SELECT * FROM user WHERE username='$nama' AND level='$jenisuser'";
	  $res=mysqli_query($conn,$q);
	  
	  if ($row=mysqli_fetch_assoc($res))
	  {
		  
	   header('location: ./regis.php?error=' .urlencode('Username sudah digunakan!'));   
	   //echo "username sama" . "</br>";
	  }	  
	  else 
	  {
	   // SQL Insert
	  
	  $status="";
	  if ($jenisuser=="3")
	  {
		  $status="0";
	  }
	  $q = "INSERT INTO user (username,password,level,status,alamat,nama_bengkel) VALUES ('$nama', '$password','$jenisuser','$status','$alamat','$namabengkel')";
	  //echo $q;
	  $insert = $conn->query($q);
	  // jika gagal
	  if (! $insert) 
	  {
		  
	   echo "<script>alert('".$conn->error."'); window.location.href = './daftarUser.php';</script>";
	   echo "gagal insert";
	  } 
	  else if ($insert)
	  {
	   //header('location: ./login.php?berhasil=' .urldecode('Akun Berhasil Didaftarkan!')); 
	   header("location:index.html?p=5");
	  }
	  }
	}
?>
