mySQL查询结果重定向到不同的网页

时间:2016-05-12 21:20:39

标签: php mysql

我目前正在尝试登录系统。用户有3个属性:id,password和userlevel。将针对不同的用户级别打开不同的网页(有3个用户级别)。我无法实现第三个功能,我只能使用id和密码登录。这是我的代码

<?php
error_reporting(E_ALL ^ E_DEPRECATED);
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="user"; // Database name
$tbl_name="test"; // Table name

$dbcon=mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$myid=$_POST['id'];
$mypassword=$_POST['pw'];

$myid = stripslashes($myid);
$mypassword = stripslashes($mypassword);
$myid = mysql_real_escape_string($myid);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE id='$myid' and password='$mypassword'";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

if($count==1){

header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}

?>

1 个答案:

答案 0 :(得分:-1)

您需要从数据库中获取用户级别,然后执行if语句,如下所示:

$sql="SELECT * FROM $tbl_name WHERE id='$myid' and password='$mypassword'";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);

if ($row['userlevel'] == 1) {
 ....
} elseif ($row['userlevel'] == 2) {
 ....
} elseif ($row['userlevel'] == 3) {
 ....
}