gpt4 book ai didi

php - select查询中的where条件

转载 作者:行者123 更新时间:2023-11-29 23:17:28 24 4
gpt4 key购买 nike

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

当我在 where 条件中添加 OR 时,它始终处于 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';
}
}

感谢您的帮助!

最佳答案

我没有发现代码有任何问题。但我只能看到逻辑是错误的。如果用户名或电子邮件已存在,您将显示一条消息:昵称用户名和电子邮件已在使用中。可能只有用户名已在使用,但电子邮件尚未使用,反之亦然。因此,您需要向用户显示正确的消息,哪一张被拍摄,哪一张未被拍摄。

<?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';
}
}
?>

关于php - select查询中的where条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27646004/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com