[英]if statement inside while loop is not working
這是一個登錄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'");
我想檢查一個用戶是否存在,然后為不同的用戶打開不同的頁面。在所有三種情況下,頁面都重定向到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>";
?>
您正在為變量$username
分配值,而不是將變量與字符串進行比較。 嘗試:
if($username=='operations')
{
header("Location: view3.php");
}
if($username=='finance')
{
header("Location: view3.php");
}
if($username=='Analyst')
{
header("Location: Analyst.php");
}
您需要使用兩個=
符號:
if($username == 'finance')
單個=
將為$username
分配一個值。 為了評估該值,應使用==
。
更好的是,您可以使用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>";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.