MySQLi prepared statement failed executing, register form

I have a problem with an prepared statement in my PHP code. It does nothing, only refreshing the page if I click on the submit button. I really don't know what the problem is, so can you guys help me?

Code:

function register($conn) {
  if (isset($_POST['submitRegister'])) {
    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $email = $_POST['email'];
    $username = $_POST['username'];
    $password = $_POST['password'];
    $confirmPassword = $_POST['confirmPassword'];
    if ($password !== $confirmPassword) {
      header("Location: ?error=cmparepasswords");
      exit();
    } else {
      $sql = "SELECT * FROM users";
      $result = mysqli_query($conn, $sql);
      while ($row = mysqli_fetch_assoc($result)) {
        $rowfullname = strtolower($row['firstname'] + " " + $row['lastname']);
        $fullname = strtolower($firstname + " " + $lastname);
        if ($rowfullname === $fullname) {
          header("Location: ?error=fullnameexists");
          exit();
        } else {
          if ($username === $row['username']) {
            header("Location: ?error=usernameexists");
            exit();
          } else {
            if ($email === $row['email']) {
              header("Location: ?error=emailexists");
              exit();
            } else {
              $sql1 = "INSERT INTO users (firstname, lastname, email, username, password, rank, ip, key) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
              $stmt = mysqli_stmt_init($conn);
              if (!mysqli_stmt_prepare($stmt1, $sql1)) {
                header("Location: ?error=sqlstatementfailed?error=" . $conn->error);
                exit();
              }
            }
          }
        }
      }
      $rank = "member";
      $password = password_hash($password, PASSWORD_BCRYPT);
      $ip = $_SERVER['REMOTE_ADDR'];
      $key = substr(str_shuffle("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-=<>'`{}[]"), 0);
      mysqli_stmt_bind_param($stmt1, "ssssssss", $firstname, $lastname, $email, $username, $password, $rank, $ip, $key);
      mysqli_stmt_execute($stmt1);
      $_SESSION['username'] = $username;
      $_SESSION['firstname'] = $firstname;
      $_SESSION['lastname'] = $lastname;
      $_SESSION['username'] = $username;
      $_SESSION['email'] = $email;
      header("Location: Index");
      exit();
    }
  }
}

My register file:

<?php
require 'header.php';
?>
<script>
  setTitle("register");
</script>
<form class="register" id="register" action="<?php register($conn); ?>" method="post">
  <h1>Register</h1>
  <input type="text" name="firstname" placeholder="Firstname" required>
  <input type="text" name="lastname" placeholder="Lastname" required>
  <input type="email" name="email" placeholder="Email" required>
  <input type="text" name="username" placeholder="Username" required>
  <input type="password" name="password" placeholder="Password" required>
  <input type="password" name="confirmPassword" placeholder="Conform password" required>
  <button type="submit" name="submitRegister">Register</button>
</form>
<?php
require 'footer.php';
?>

I'm a bit new with prepared statement so I'm trying to understand what I'm doing wrong.

728x90

0 Answers MySQLi prepared statement failed executing, register form