如何停止在PHP中存储现有的用户名和电子邮件?

时间:2014-03-16 22:46:57

标签: php html mysql email

我想阻止用户在数据库中存储现有的电子邮件地址和用户名。我尝试使用下面的if语句:如果用户名和电子邮件都存在行,那么echo语句。否则,如果数据库中不存在用户名和电子邮件,则插入。

然而,它似乎仍然存储数据。

    $selectUser= mysql_query("SELECT * FROM members WHERE username = '$username'") or die (mysql_error());
    $selectEmail= mysql_query("SELECT * FROM members WHERE email = '$email'") or die (mysql_error());

    if(mysql_num_rows($selectUser) > 0) {
        echo 'Wrong Username!';
    }
    if(mysql_num_rows($selectEmail) > 0) {
        echo 'Wrong Email!';
    } 
    mysql_query("INSERT INTO members (username,password,email) VALUES ('$username','$password','$email')") or die (mysql_error());
        echo 'Congrats! You have registed!';
        exit();

4 个答案:

答案 0 :(得分:1)

问题是在回显错误消息后仍然执行insert语句。试试这个:

$selectUser= mysql_query("SELECT * FROM members WHERE username = '$username'") or die (mysql_error());
$selectEmail= mysql_query("SELECT * FROM members WHERE email = '$email'") or die (mysql_error());

$error = false;
if(mysql_num_rows($selectUser) > 0) {
    echo 'The username is already taken.';
    $error = true;
}
if(mysql_num_rows($selectEmail) > 0) {
    echo 'The email has already been used by another member.';
    $error = true;
} 
if(!$error) {
    mysql_query("INSERT INTO members (username,password,email) VALUES ('$username','$password','$email')") or die (mysql_error());
    echo 'Congrats! You\'ve registed!';
    exit();
}

如果你不喜欢$error变量,你可以这样做

$selectUser= mysql_query("SELECT * FROM members WHERE username = '$username'") or die (mysql_error());
$selectEmail= mysql_query("SELECT * FROM members WHERE email = '$email'") or die (mysql_error());

if (mysql_num_rows($selectUser) > 0) {
    echo 'The username is already taken.';
} elseif (mysql_num_rows($selectEmail) > 0) {
    echo 'The email has already been used by another member.';
} else {
    mysql_query("INSERT INTO members (username,password,email) VALUES ('$username','$password','$email')") or die (mysql_error());
    echo 'Congrats! You\'ve registed!';
    exit();
}

与您的问题无关,但非常重要的是您应该验证输入($username$password$email)。你可以为此use mysql_real_escape_string(如果使用PHP> = 5.0,则为mysqli_real_escape_string)。

答案 1 :(得分:1)

如果您坚持使用2个查询,请尝试此操作。如果您觉得需要分开"重复用户名"和#34;重复电子邮件"错误,只是在那里得到另一个{}。

$selectUser= mysql_query("SELECT * FROM members WHERE username = '$username'") or die (mysql_error());
$selectEmail= mysql_query("SELECT * FROM members WHERE email = '$email'") or die (mysql_error());

if(mysql_num_rows($selectUser) > 0 or mysql_num_rows($selectEmail) > 0) {
    echo 'Duplicate Username or Email!';
} else {
    mysql_query("INSERT INTO members (username,password,email) VALUES ('$username','$password','$email')") or die (mysql_error());
    echo 'Congrats! You\'ve registed!';
    exit();
}

否则,请使用Logan Wayne或PeaceDealer的1个查询答案。

编辑:单独的用户名和电子邮件

$selectUser= mysql_query("SELECT * FROM members WHERE username = '$username'") or die (mysql_error());
$selectEmail= mysql_query("SELECT * FROM members WHERE email = '$email'") or die (mysql_error());

if(mysql_num_rows($selectUser) > 0 or mysql_num_rows($selectEmail) > 0) {
    if(mysql_num_rows($selectUser) > 0) {
        echo 'Duplicate Username';
    }
    if(mysql_num_rows($selectEmail) > 0) {
        echo 'Duplicate email';
    }
} else {
    mysql_query("INSERT INTO members (username,password,email) VALUES ('$username','$password','$email')") or die (mysql_error());
    echo 'Congrats! You\'ve registed!';
    exit();
}

答案 2 :(得分:0)

另一种示例方法:

$select= mysql_query("SELECT * FROM members WHERE username = '". $username ."' OR  email = '". $email ."'") or die (mysql_error());

if(mysql_num_rows($select) > 0) {
echo "Username or email taken";
} else {    
    mysql_query("INSERT INTO members (username,password,email) VALUES ('$username','$password','$email')") or die (mysql_error());
    echo 'Congrats! You\'ve registed!';
}

但正如其他人所述,你应该通过示例转义字符串来验证输入。

答案 3 :(得分:0)

您只需一次查询即可。

$select=mysqli_query($con,"SELECT * FROM members WHERE username = '$username' OR email='$email'");

if(mysqli_num_rows($select)==0){

mysqli_query($con,"INSERT INTO members (username,password,email) VALUES ('$username','$password','$email')");

}

else {

while($row=mysqli_fetch_array($result)){

if($row['username']==$username){
   echo "Error: Existing Username";
}

if($row['email']==$email){
   echo "Error: Existing Email Address";
}

}    /* END OF WHILE LOOP */

}    /* END OF ELSE */

请使用最新的MySQLi。