Try to search your question here, if you can't find : Ask Any Question Now ?

PHP file to check if email exists

HomeCategory: stackoverflowPHP file to check if email exists
Avatarrupesh asked 2 weeks ago

I`m trying to check if an email already exists in my database, I thought I had the right code to check, but for some reason, I can still create another user with the same email.

This is the code that i currently have:

<?php
  if (empty($username) || empty($email) || empty($pwd) || empty($pwdcheck)){
    header("Location: ../cadastrar.php?error=emptyfields&nome=".$username."&email=".$email);
    exit();
  }else if(!filter_var($email, FILTER_VALIDATE_EMAIL) && !preg_match("/^[a-zA-Z0-9]*$/", $username)){
    header("Location: ../cadastrar.php?error=invalidmailusername");
    exit();
  }else if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
    header("Location: ../cadastrar.php?error=invalidmail&nome=".$username);
    exit();
  }else if(!preg_match("/^[a-zA-Z0-9]*$/", $username)){
    header("Location: ../cadastrar.php?error=invalidusername&email=".$email);
    exit();
  }else if($pwd !== $pwdcheck){
    header("Location: ../cadastrar.php?error=passwordcheck&email=".$email."&nome=".$username);
    exit();
  }else{
    $sql = "SELECT usuario FROM users WHERE usuario=?";
    $stmt = mysqli_stmt_init($conn);
    if (!mysqli_stmt_prepare($stmt, $sql)){
      header("Location: ../cadastrar.php?error=sqlerror");
      exit();
    }else{
      mysqli_stmt_bind_param($stmt, "s", $username);
      mysqli_stmt_execute($stmt);
      mysqli_stmt_store_result($stmt);
      $resultCheck = mysqli_stmt_num_rows($stmt);
      if ($resultCheck > 0){
        header("Location: ../cadastrar.php?error=usertaken&email=".$email);
        exit();
      }else{
        $sql = "INSERT INTO users (nomecompleto ,usuario, emailuser, pwdusers) VALUES (?, ?, ?, ?)";
        $stmt = mysqli_stmt_init($conn);
        if (!mysqli_stmt_prepare($stmt, $sql)){
          header("Location: ../cadastrar.php?error=sqlerror");
          exit();
        }else{
          $hashedpwd = password_hash($pwd, PASSWORD_DEFAULT);

          mysqli_stmt_bind_param($stmt, "ssss", $nomecompleto, $username, $email , $hashedpwd);
          mysqli_stmt_execute($stmt);
          header("Location: ../cadastrar.php?cadastro=sucesso");
          exit();
        }
      }
    }
  }
  mysqli_stmt_close($stmt);
  mysqli_close($conn);
}else{
  header("Location: ../cadastrar.php");
  exit();
}
1 Answers
Best Answer
AvatarJyoti answered 2 weeks ago
Your Answer

20 + 11 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com