【发布时间】:2019-10-02 04:51:54
【问题描述】:
我正在尝试检查我的数据库中是否已经存在电子邮件,我认为我有正确的代码可以检查,但由于某种原因,我仍然可以使用相同的电子邮件创建另一个用户。我想同时检查用户名和电子邮件。
这是我目前拥有的代码:
<?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();
}
【问题讨论】: