Data tidak bisa masuk ke dalam database

Jadi Saya Membuat form registrasi, ketika saya coba notifikasi nya berhasil masuk, tetapi data nya gamasuk ke dalam database kira kira gimana ya thks.

Ini Kode function.php

<?php
$con = mysqli_connect("localhost","root","","db_notariat");
function registrasi($data){
	global $con;

	$username = strtolower(stripslashes($data["username"]));
	$password = ($data["password"]);
	$password2 = ($data["password2"]);
	//cek username apakah sudah terdaftar atau belum
	$result  = mysqli_query($con,"SELECT username FROM tb_user WHERE username = '$username'");
	if (mysqli_fetch_assoc($result)) {
		echo "<script>alert('Username Yang Anda Masukkan Sudah Terdaftar')</script>";
		return false;
	}
	//cek konfirmasi password

	if ($password !== $password2) {
			echo "<script>alert('Konfirmasi password Tidak Sesuai!');</script>";
			return false;
	}else{
		return 1;
	}

	//encrypt password
	$password = password_hash($password, PASSWORD_DEFAULT);
		// var_dump($password);die;

	//tambahkan user baru ke dalam database
	 mysqli_query($con,"INSERT INTO tb_user VALUES('','$username','$password')");
   return mysqli_affected_rows($con);
}
 ?>

Ini Form regis.php

<?php
$con = mysqli_connect("localhost","root","","db_notariat");
include "function.php";

if (isset($_POST['register'])) {
    if (registrasi($_POST) > 0) {
          echo "<script>alert('Berhasil Mendaftar');document.location.href = 'login.php'</script>";
    }else{
      echo mysqli_error($con);

    }
}

 ?>
<!doctype HTML>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">

    <title>Registrasi || Notaris</title>
  </head>
  <body>
    <nav class="navbar navbar-light bg-light">
  <a class="navbar-brand" href="#">Dewi Rini Herlina SH.MH.</a>
</nav>
<br>
<!-- REgist -->
<div class="container">
     <div class="col-lg-6 s12 text-white hero" style="background-color:#57585b; border-radius: 20px;">
    <form method="post">

  <div class="form-group">
    <label for="username">Username</label>
    <input type="text" class="form-control" name="username" id="username" aria-describedby="emailHelp" placeholder="Masukkan Username">
  </div>
  <div class="form-group">
    <label for="password">Password</label>
    <input type="password" class="form-control" name="password" id="password" placeholder="Masukkan Password">
  </div>
  <div class="form-group">
    <label for="password2"> Konfirmasi Password</label>
    <input type="password" class="form-control" name="password2" id="password2" placeholder="Masukkan Password">
  </div>
  <div class="form-group form-check">
  <a href="login.php" style="color: white; text-decoration: none;">Sudah Punya Akun?</a>
  </div>
   <button type="submit" name="register" value="Login" class="btn btn-outline-light" onclick="sweet()">Daftar</button>
</form>
</div>
</div>
</div>
</div>
    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script>

    <script src="js/dist/sweetalert2.all.marginn.js"></script>
    <script src="js/dist/sweetalert2.min.js"></script>

    <link rel="stylesheet" type="text/css" href="js/dist/sweetalert2.min.css">
    <style type="text/css">
      .hero{
        padding: 20px;
        border-radius: 20px;
        margin: 20px auto;
        box-shadow: 0px 0px 10px 0px;
        box-sizing: border-box;

      }
      body{
        background-image: url(img/g3.jpg);
      }
    </style>
  </body>
</html>

avatar jiasfjabfie1212
@jiasfjabfie1212

38 Kontribusi 0 Poin

Diperbarui 5 tahun yang lalu

6 Jawaban:

Jawaban Terpilih

<pre> if ($password !== $password2) { echo "&lt;script&gt;alert('Konfirmasi password Tidak Sesuai!');&lt;/script&gt;"; return false; }else{ return 1; } </pre> kenapa yang satu return boolean yang satu return integer?dipengecekanmu registrasi($_POST) &gt; 0, kalau hasil return an nya boolean maka false &gt; 0 kira2 hasilnya seperti apa?klo udah return codingan dibawah nya ga akan kena gan,

<pre> function registrasi($data){ global $con;

$username = strtolower(stripslashes($data[&quot;username&quot;]));
$password = ($data[&quot;password&quot;]);
$password2 = ($data[&quot;password2&quot;]);
//cek username apakah sudah terdaftar atau belum
$result  = mysqli_query($con,&quot;SELECT username FROM tb_user WHERE username = &#039;$username&#039;&quot;);
if (mysqli_fetch_assoc($result)) {
	echo &quot;&amp;lt;script&amp;gt;alert(&#039;Username Yang Anda Masukkan Sudah Terdaftar&#039;)&amp;lt;/script&amp;gt;&quot;;
	return 0;
} else {
	if ($password !== $password2) {
		echo &quot;&amp;lt;script&amp;gt;alert(&#039;Konfirmasi password Tidak Sesuai!&#039;);&amp;lt;/script&amp;gt;&quot;;
		return 0;
	}else{
		//encrypt password
		$password = password_hash($password, PASSWORD_DEFAULT);
		// var_dump($password);die;

		//tambahkan user baru ke dalam database
		mysqli_query($con,&quot;INSERT INTO tb_user VALUES(&#039;&#039;,&#039;$username&#039;,&#039;$password&#039;)&quot;);
		return mysqli_affected_rows($con);
	}
}

} </pre>

avatar kikimf
@kikimf

91 Kontribusi 78 Poin

Dipost 5 tahun yang lalu

tapi malah gabisa login login nya harus pake password yang sudah di encrypt misal

password : 123 yang di encrypt : $2y$10$n5zgb4zr8Kmo1ZFBMRpl3uPjywQCvF4D8.R57Jn45Ez

jadi di form login nya kita harus memasukan password :

$2y$10$n5zgb4zr8Kmo1ZFBMRpl3uPjywQCvF4D8.R57Jn45Ez

Bukan : 123

kira kira gimana ya

avatar jiasfjabfie1212
@jiasfjabfie1212

38 Kontribusi 0 Poin

Dipost 5 tahun yang lalu

tapi malah gabisa login login nya harus pake password yang sudah di encrypt misal

password : 123 yang di encrypt : $2y$10$n5zgb4zr8Kmo1ZFBMRpl3uPjywQCvF4D8.R57Jn45Ez

jadi di form login nya kita harus memasukan password :

$2y$10$n5zgb4zr8Kmo1ZFBMRpl3uPjywQCvF4D8.R57Jn45Ez

Bukan : 123

kira kira gimana ya

avatar jiasfjabfie1212
@jiasfjabfie1212

38 Kontribusi 0 Poin

Dipost 5 tahun yang lalu

Ketika user ingin login password yang user input dihash lagi kemudian baru cocokan dengan database

avatar kikimf
@kikimf

91 Kontribusi 78 Poin

Dipost 5 tahun yang lalu

boleh minta code nya?

avatar jiasfjabfie1212
@jiasfjabfie1212

38 Kontribusi 0 Poin

Dipost 5 tahun yang lalu

kalau kaya gini bener?

<pre> if (isset($_POST["masuk"])) { $query = "SELECT * FROM tb_user WHERE username = '$_POST[username]' AND password = '$_POST[password]'"; $result = mysqli_query($con,$query); if (mysqli_num_rows($result) === 1) { //cek password $row = mysqli_fetch_assoc($result); if(password_verify($_POST[password], $row["password"])){ header("location: index.php"); exit; } } $error = true; } </pre>

avatar jiasfjabfie1212
@jiasfjabfie1212

38 Kontribusi 0 Poin

Dipost 5 tahun yang lalu

Tanggapan

coba aja gan pake itu, seharusnya sih bisa

Login untuk ikut Jawaban