使用2个单独的查询来检查数据库中是否存在电子邮件和用户名

时间:2017-07-18 16:16:10

标签: php mysqli phpmyadmin

我正在尝试让我的PHP脚本检查数据库中是否已经存在电子邮件和用户名,然后如果他们这样做则会显示消息 - 我最初只能检查一个成功但现在我正在尝试在插入之前检查两者我遇到了一些问题。

有人可以就我在这里做错了什么给我一些建议吗?

// Check if simple anti-bot test is correct
if ($human !== 5) {
    $errHuman = 'Your anti-spam is incorrect';
}
else {
    if (!$errEmail && !$errUsername && !$errPassword && !$errPassMatch && !$errHuman) {

        $query1="SELECT email FROM blog_members WHERE email='$email'";
        $query2="SELECT username FROM blog_members WHERE username='$signupusername'";
        $result1=mysqli_query($conn, $query1);
        $result2=mysqli_query($conn, $query2);

        if ($result1 && mysqli_num_rows($result1) >=1) {
            $error = "Email already exists!";
        }else if ($result2 && mysqli_num_rows($result2) >=1 ) {
            $error = "Username already exists!"
        }else {
            $query="INSERT INTO blog_members(username, password, email) VALUES ('$signupusername', '$signuppassword', '$email')";
            $result=mysqli_query($conn, $query);
            ?>
              <script type="text/javascript">
                alert("User created, please login from the homepage!");
                window.location.href = 'http://www.mywebsite.co.uk/index.php#login';
              </script>
            <?php
        }
    }
}

1 个答案:

答案 0 :(得分:0)

尝试这样做:

$query = mysqli_query("SELECT * FROM blog_members WHERE username='$username' OR email='$email'", $conn);
$result = mysqli_num_rows($query);

if ($result != 0) {
    // What happens if it exists
} else {
    // What happens if it doesn't exist
}

告诉我发生的任何错误或出现任何问题。