select查询中的条件

时间:2014-12-25 09:36:54

标签: php mysql sql database

如果两者中的任何一个已经存在,我必须检查用户名和电子邮件,

当我在OR条件中添加where时,它始终处于IF状态。

代码:

$fetch_user=mysql_query("SELECT userid FROM users WHERE username ='".$username."' OR email='".$email."'") or die(mysql_error());
if(mysql_num_rows($fetch_user))
{ 
    echo '<font color="red">The nickname <strong>'.$username.''.$email.'</strong>'.' is already in use.</font>';
} 
else 
{
    $query = mysql_query("insert into users values ('','$username','$email','$password','','$roll')") or die(mysql_error());
    if($query)
    {
        echo 'Successfully Inserted';
    }
    else
    {
        echo 'Insertion Faild';
    }
}

感谢您的帮助!

3 个答案:

答案 0 :(得分:0)

仅更改条件

    $fetch_user=mysql_query("SELECT userid FROM users WHERE username ='".$username."' OR email='".$email."'") or die(mysql_error());
    $num = mysql_num_rows($fetch_user);
    if($num > 1)
    { 
        echo '<font color="red">The nickname <strong>'.$username.''.$email.'</strong>'.' is already in use.</font>';
    } 
    else 
    {
        $query = mysql_query("insert into users values ('','$username','$email','$password','','$roll')") or die(mysql_error());
        if($query)
        {
            echo 'Successfully Inserted';
        }
        else
        {
            echo 'Insertion Faild';
        }
    }

答案 1 :(得分:0)

您没有验证来自SQL查询的结果。 以下代码将起作用

$fetch_user=mysql_query("SELECT userid FROM users WHERE username ='".$username."' OR email='".$email."'") or die(mysql_error());
if( !$fetch_user)
{
	if(mysql_num_rows($fetch_user) > 0 )
	{ 
		echo '<font color="red">The nickname <strong>'.$username.''.$email.'</strong>'.' is already in use.</font>';
	} 

else 
{
    $query = mysql_query("insert into users values ('','$username','$email','$password','','$roll')") or die(mysql_error());
    if($query)
    {
        echo 'Successfully Inserted';
    }
    else
    {
        echo 'Insertion Faild';
    }
}
}
else
[
echo 'query failed';
}

另请查看以下文档链接,以便更好地了解功能 http://php.net/manual/en/function.mysql-num-rows.php http://php.net/manual/en/function.mysql-query.php

答案 2 :(得分:0)

我在代码中没有看到任何错误。但是我只能看到逻辑错误。如果用户名或电子邮件已存在,则表示您显示nickname username and email already in use消息。有可能只有用户名已被使用但电子邮件没有,反之亦然。因此,您需要向用户显示正确的消息,哪一个被拍摄,哪一个不被拍摄。

<?php 
$userNameTaken = false;
$emailTaken = false;

//check if username exists
$checkUsername = mysql_query("SELECT userid FROM users WHERE username ='".$username."'") or die(mysql_error());
if(mysql_num_rows($checkUsername))
{ 
    $userNameTaken = true;
}

//check if email exists
$checkEmail = mysql_query("SELECT userid FROM users WHERE email ='".$email."'") or die(mysql_error());
if(mysql_num_rows($checkEmail))
{ 
    $emailTaken = true;
}

if($userNameTaken || $emailTaken) //if any of username or email taken
{
    if($userNameTaken && $emailTaken) //username and email both taken
    {
        echo '<font color="red">The username <strong>'.$username.'</strong> and email <strong>'.$email.'</strong> already in use.</font>';
    }
    else if($userNameTaken && !$emailTaken) //only username taken
    {
        echo '<font color="red">The username <strong>'.$username.'</strong> is already in use.</font>';
    }
    else if(!$userNameTaken && $emailTaken) //only email taken 
    {
        echo '<font color="red">The email <strong>'.$email.'</strong> is already in use.</font>';
    }
} 
else 
{
    $query = mysql_query("insert into users values ('','$username','$email','$password','','$roll')") or die(mysql_error());
    if($query)
    {
        echo 'Successfully Inserted';
    }
    else
    {
        echo 'Insertion Faild';
    }
}
?>