以下代码有什么问题?请帮帮我。
我希望将数据库中的admin-id和密码与普通用户的login-id和密码进行匹配,并希望将控件转移到相应的表单。
当我运行此代码时,它会出现以下错误:
注意:未定义的变量:第25行的C:\ xampp \ htdocs \ xampp \ Test \ HRMS \ extract.php中的userstatus
注意:未定义的变量:第30行的C:\ xampp \ htdocs \ xampp \ Test \ HRMS \ extract.php中的usertype
$query1="select user_type,user_staus from `user_info` where name='$username' and
password='$password'";
$fetched=mysql_query($query1);
while($record=mysql_fetch_assoc($fetched))
{
while(each($record))
{
$usertype=$record["user_type"];
$userstatus=$record["user_staus"];
}//closing of 1st while loop
}//closing of 2nd while loop
if($userstatus==1) //if is logged in already
{
echo "Please login after some time";
exit();
}
if($usertype == 0) // if user is not an admin
{
$query1="select * from `user_info` where name='$username' and password='$password'";
$result = mysql_query($query1);
if(mysql_num_rows($result) == 1)
{
header("Location: user_form.php");
}
}
else if($usertype == 1) //if the user is a normal user
{
header("Location: admin_form.php");
}
else
{
echo "please register to login";
}
有人可以帮我找到问题吗?
答案 0 :(得分:1)
您的代码存在许多问题,因为您收到错误的主要原因是$usertype
和$userstatus
未预定义且未经过验证。
但在我看来,这不是您的代码的主要问题。
我想问你几个问题:
mysql_real_escape_string
方法转义了$username
和$password
个不良字符?以下是此代码的示例:
$query1 = "SELECT user_type,user_staus FROM `user_info` WHERE name='{$username}' AND password='{$password}' LIMIT 1";
$fetched = mysql_query($query1);
//check if record exists otherwise you would receive another notice that can
//break redirect functionality
if (mysql_num_rows($fetched))
{
$record = mysql_fetch_assoc($fetched);
// make sure that value is integer
if ((int)$record["user_staus"])
{
exit("Please login after some time");
}
else
{
$url = (bool)$record["user_type"] ? 'admin_form.php' : 'user_form.php';
header("Location: {$url}");
exit(0);
}
}
else
{
echo "please register to login";
}
<强>更新强>
根据{{3}}的建议,删除了第3级if
嵌套并替换为三元比较
答案 1 :(得分:0)
您忽略了范围规则(因为您没有显示完整代码)
while($record=mysql_fetch_assoc($fetched))
{
while(each($record))
{
$usertype=$record["user_type"];
$userstatus=$record["user_staus"];
}//closing of 1st while loop
} //关闭第二个while循环
这里$ usertype和$ userstatus在内部while循环{}内声明。 即,他们的范围转向{}。一旦代码出来,$ userstatus和$ usertype就会死掉,因此无法进一步访问。
你必须首先在全局区域声明变量ut。