电子邮件验证无法正常运行

时间:2016-02-02 20:08:46

标签: php mysqli

我有这段代码:

$checkDB = "SELECT email FROM users WHERE email='$email'";
$result = mysqli_query($conn, $checkDB);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);

if (mysqli_num_rows($result) == 1) {
    $errorEmail = 'This email is registered m8';
    $mainError = true;
}

所以问题是它总是说电子邮件已经注册,即使它不是用于电子邮件检查(电子邮件检查是在名称检查之后)。我尝试更改变量名称,但它不起作用。

$checkDB = "SELECT user_name FROM users WHERE user_name='$name'";
    $result = mysqli_query($conn, $checkDB);
    $row = mysqli_fetch_array($result, MYSQLI_ASSOC);

    if (mysqli_num_rows($result) == 1) {
        $errorName = 'Name registered';
        $mainError = true;
    }

2 个答案:

答案 0 :(得分:0)

mysqli_num_rows($result);检查找到的电子邮件的行数。并且不要放mysqli_fetch_array($result, MYSQLI_ASSOC);。我不知道为什么,但它对我有用。

使用此代码

$checkDB = "SELECT email FROM users WHERE email='$email'";
$result = mysqli_query($conn, $checkDB);
$check = mysqli_num_rows($result);

if ( $check == 1) {
    $errorEmail = 'This email is registered m8';
    $mainError = true;
}

答案 1 :(得分:0)

  • 您应该检查它是否大于零
  • 没有必要提取
  • 同样在你的情况下,你应该准备你的声明。

$checkDB = "SELECT email FROM users WHERE email= ?";
if ($result = mysqli_prepare($conn, $checkDB)) {
    mysqli_stmt_bind_param($result, "s", $email);
    mysqli_stmt_execute($result );
    if(mysqli_num_rows($result) > 0){
        $errorName = 'Name registered';
        $mainError = true;
    }
}
mysqli_close($conn);