为什么输入正确时会触发我的 if else if 语句?

Why are my if elseif statemtnes being triggered when input entered is correct?

我每次点击提交时都会触发我的 if elseif 语句,而且它只是特定的语句。我已经检查了 Stack 是否存在类似问题,但它们都与未触发的语句有关。我还尝试多次验证我的表单,无论对错,都会触发这些特定的表单:

用户名:strlen语句
密码:strlen语句
电子邮箱:FILTER_VALIDATE_EMAIL
即使所有字段都已填充,也会触发空检查

非常感谢任何有关更正此问题的帮助。

//Spam Check
if($spam > 2){
    $error .= '<div class="problem">Too many accounts have been created from this IP Address. Please contact us if you would like to create more.</div>';
}

//Username Check
if(!preg_match("/^[a-zA-Z0-9 ]*$/",$user)){
    $error .= '<div class="problem">You may only use letters and numbers in your username.</div>';
}
elseif(strlen($user) < 3 OR strlen($user) > 20){
    $error .= '<div class="problem">Please use an username between 3 and 20 characters.</div>';
}
elseif($taken > 0){
    $error .= '<div class="problem">This username has already been taken. Please choose another.</div>';
}

//Password Check
if(!preg_match("/^[a-zA-Z0-9 ]*$/",$pass1)){
        $error .= '<div class="problem">Do not use special characters in your password.</div>';
}
elseif(strlen($pass1) < 5 OR strlen($pass1) > 20){
    $error .= '<div class="problem">Your password must be at least 5 characters and no more than 20.</div>';
}
elseif($pass1 != $pass2){
    $error .= '<div class="problem">You did not confirm your passwords correctly. Please make sure they are the same.</div>';
}

//E-Mail Check
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
    $error .= '<div class="problem">The E-Mail Address entered was invalid. Please use another.</div>';
}
elseif(strlen($email) > 50){
        $error .= '<div class="problem">This E-Mail Address is too large for us to store. Please use a different one.</div>';
}

//Empty Check
if(empty($user) OR empty($pass1) OR empty($pass2) OR empty($email)){
        $error .= '<div class="problem">You need to fill out all fields to create an account.</div>';
}



//Variables
$user = mysqli_real_escape_string($_POST['username']);
$pass1 = mysqli_real_escape_string($_POST['pass1']);
$pass2 = mysqli_real_escape_string($_POST['pass2']);
$email = mysqli_real_escape_string($_POST['email']);

您正在 POST 数据上使用 mysqli 函数 -- 您的变量不是来自 SQL,是吗?使用 trim() 和 strip_tags() 或类似的东西来清理您的数据(但是开始清理并使用 mysqli 而不是 mysql 函数的荣誉。)

对所有变量尝试类似的操作,看看您的 IF 语句是否开始正确计算。如果它们都是 NULL,它们现在可能都返回 false。

$user = strip_tags(trim($_POST['username']));