Membuat User Registration PHP dan MYSQL

Mengapa user registration tidak bisa dibuat dan selalu mengeluarkan notifikasi seperti ini "Database error: failed to register" ? Apakah ada yang salah dengan kodingan saya ?

file koneksi.php

<?php
  define('DB_HOST', 'localhost');
  define('DB_USER', 'root');
  define('DB_PASS', '');
  define('DB_NAME', 'user-verification');

  $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

  if($conn -> connect_error){
    die("Database error:" . $conn -> connect_error);  }

file controller.php

<?php
  session_start();

  require "koneksi.php";

  $errors = array();
  $username = "";
  $email = "";

  if (isset($_POST["register"])){
    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $password_konfirm = $_POST['password_konfirm'];

    if (empty($username)) {
      $errors['username'] = "Membutuhkan username!";
    }if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $errors['email'] = "email yang dimasukkan invalid!";
    }if (empty($email)) {
      $errors['email'] = "Membutuhkan email!";
    }if (empty($password)) {
      $errors['password'] = "Membutuhkan password!";
    }if ($password !== $password_konfirm) {
      $errors['password'] = "password dan password konfirmasi tidak sama!";
    }

    $emailQuery ="SELECT * FROM users WHERE email=? LIMIT 1";
    $stmt = $conn->prepare($emailQuery);
    $stmt->bind_param('s', $email);
    $stmt->execute();
    $result = $stmt->get_result();
    $userCount = $result->num_rows;
    $stmt->close();

    if ($userCount > 0) {
      $errors['email'] = "Email sudah ada";
    }

    if (count($errors) === 0) {
      $password = password_hash($password, PASSWORD_DEFAULT);
      $token = bin2hex(random_bytes(50));
      $verified = false;

      $sql = "INSERT INTO users (username, email, verified, token, password) VALUES (?, ?, ?, ?, ?)";
      $stmt = $conn->prepare($sql);
      $stmt->bind_param('ssbss', $username, $email, $verified, $token, $password);

      if ($stmt->execute()){
        $user_id = $conn->insert_id;
        $_SESSION['id'] = $user_id;
        $_SESSION['username'] = $user_username;
        $_SESSION['email'] = $user_email;
        $_SESSION['verified'] = $user_verified;
        $_SESSION['message'] = "Silahkan login sekarang!";
        $_SESSION['alert-class'] = "alert-success";
        header('location: index.php');
        exit();
      }else {
        $errors['db_error'] = "Database error: failed to register";
      }
    }
  }

?>

file register.php

<?php require_once "header.php"; ?>
<?php require "controller.php"; ?>
  <div class="container">
    <div class="row">
      <div class="col-md-8 offset-md-2">
        <div class="card">
          <div class="card-header bg-info">
            <h3 class="display-4 text-light">Daftar</h3>
          </div>
          <div class="card-body">
            <?php if(count($errors) > 0): ?>
              <div class="alert alert-danger">
                <?php foreach($errors as $error){ ?>
                  <li><?php echo $error; ?></li>
                <?php } ?>
              </div>
            <?php endif; ?>
            <form action="register.php" method="POST">
              <div class="form-group">
                <label>Username</label>
                <input type="text" name="username" value="<?php echo $username; ?>" class="form-control form-control-lg">
              </div>
              <div class="form-group">
                <label>Email</label>
                <input type="email" name="email" value="<?php echo $email; ?>" class="form-control form-control-lg">
              </div>
              <div class="form-group">
                <label for="password">Password</label>
                <input for="password" type="password" name="password" class="form-control form-control-lg">
              </div>
              <div class="form-group">
                <label>Password Konfirmasi</label>
                <input type="password" name="password_konfirm" class="form-control form-control-lg">
              </div>
              <div class="form-group">
                <button type="submit" name="register" class="btn btn-info btn-block btn-md">Daftar</button>
              </div>
              <p class="text-small">Sudah memiliki akun? <a href="login.php">Login</a></p>
            </form>
          </div>
        </div>

      </div>
    </div>
  </div>

<?php require_once "footer.php"; ?>

file index.php

<?php require_once "header.php"; ?>

<?php require "controller.php"; ?>
  <div class="container">
    <div class="row">
      <div class="col-md-8 offset-md-2">
        <div class="card border-secondary">
          <div class="card-body">
            <div class='alert <?php echo $_SESSION['alert-class']; ?>' >
              <?php echo $_SESSION['message'] ?>
            </div>

            <h2 class="display-3 mb-4">Selamat Datang, <?php echo $_SESSION['username']; ?> </h2>

            <a href="#" class="text-danger">logout</a>

            <?php if(!$_SESSION['verified']): ?>
              <div class="alert alert-warning mt-4">
                Anda perlu memverifikasi akun Anda. Dengan asuk ke akun email Anda dan klik tautan verifikasi yang baru saja kami kirimi Anda email di <strong>admin@gmail.com</strong>
              </div>

              <button type="button" class="btn btn-block btn-lg btn-success mt-4">Saya sudah verifikasi!</button>
            <?php endif; ?>

          </div>
        </div>
      </div>
    </div>
  </div>

<?php require_once "footer.php"; ?>

Mohon bantuannya;) ...

avatar sweetcorn123
@sweetcorn123

15 Kontribusi 2 Poin

Dipost 3 tahun yang lalu

Belum ada Jawaban. Jadi yang pertama Jawaban

Login untuk ikut Jawaban