[英]if statement inside while loop is not working
This is a login php code that allows different users to login and use the application 这是一个登录php代码,允许不同的用户登录和使用该应用程序
$con=mysql_connect("localhost","root","");
mysql_select_db("sg",$con);
if(isset($_POST['uid'])){ $username = $_POST['uid']; }
if(isset($_POST['pwd'])){ $password = $_POST['pwd']; }
error_reporting(0);
$result=mysql_query("SELECT username,password from user where username = '$username' and password='$password'");
I want to check if a user exists and then open different pages for different users.In all three cases the page is redirected to Analyst.php 我想检查一个用户是否存在,然后为不同的用户打开不同的页面。在所有三种情况下,页面都重定向到Analyst.php
while($info = mysql_fetch_array( $result ))
{
if($username='operations')
{
header("Location: view3.php");
}
if($username='finance')
{
header("Location: view3.php");
}
if($username='Analyst')
{
header("Location: Analyst.php");
}
}
echo "<script type='text/javascript'>alert('Invalid userID or password ');window.location=\"home.html\";</script>";
?>
You are assigning values to variable $username
instead of comparing the variable to string. 您正在为变量$username
分配值,而不是将变量与字符串进行比较。 Try with : 尝试:
if($username=='operations')
{
header("Location: view3.php");
}
if($username=='finance')
{
header("Location: view3.php");
}
if($username=='Analyst')
{
header("Location: Analyst.php");
}
You need to use two =
signs: 您需要使用两个=
符号:
if($username == 'finance')
A single =
will assign a value to $username
. 单个=
将为$username
分配一个值。 In order to assess the value, you should use ==
. 为了评估该值,应使用==
。
Better still, you could use a switch()
statement: 更好的是,您可以使用switch()
语句:
while($info = mysql_fetch_array( $result ))
{
switch($username)
{
case 'operations':
case 'finnce':
header("Location: view3.php");
break;
case 'Analyst':
header("Location: Analyst.php");
break;
}
}
echo "<script type='text/javascript'>alert('Invalid userID or password ');window.location=\"home.html\";</script>";
Please also be advised that the mysql_*
family of functions is now deprecated, and should not be used in new code. 还请注意, mysql_*
系列函数现已被弃用,不应在新代码中使用。 You should instead look at MySQLi or PDO . 您应该改为查看MySQLi或PDO 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.