我的问题可能只是一个简单的问题给你的专家,但我仍然无法理解在哪里放这个循环代码
我应该把它放在哪里:
while($row=mysql_fetch_array($query))
{
if($row['level']==1)
{
header('Location: home.php');
}
else if($row['level']==2)
{
header('Location: home2.php');
}
else if($row['level']==3)
{
header('Location: home3.php');
}
}
在此:
`<?php
session_start();
$id = mysql_real_escape_string($_POST['id']);
$katalaluan = mysql_real_escape_string($_POST['katalaluan']);
mysql_connect("localhost", "root","") or die(mysql_error()); //Connect to server
mysql_select_db("sistem_laporan") or die("Cannot connect to database"); //Connect to database
$query = mysql_query("SELECT * from profil WHERE id='$id'"); //Query the profil table if there are matching rows equal to $id
$exists = mysql_num_rows($query); //Checks if id exists
$table_profil = "";
$table_katalaluan = "";
if($exists > 0) //IF there are no returning rows or no existing id
{
while($row = mysql_fetch_assoc($query)) //display all rows from query
{
$table_profil = $row['id']; // the first id row is passed on to $table_profil, and so on until the query is finished
$table_katalaluan = $row['katalaluan']; // the first password row is passed on to $table_profil, and so on until the query is finished
}
if(($id == $table_profil) && ($katalaluan == $table_katalaluan)) // checks if there are any matching fields
{
if($katalaluan == $table_katalaluan)
{
$_SESSION['id'] = $id; //set the id in a session. This serves as a global variable
header("location: home.php"); // redirects the user to the authenticated home page
}
}
else
{
Print '<script>alert("Kata Laluan dimasukkan adalah salah!");</script>'; //Prompts the user
Print '<script>window.location.assign("login.php");</script>'; // redirects to login.php
}
}
else
{
Print '<script>alert("ID Pengguna dimasukkan adalah salah!");</script>'; //Prompts the user
Print '<script>window.location.assign("login.php");</script>'; // redirects to login.php
}
?>`
主要的是我想将不同级别(1-admin 2-assistant 3-director)重定向到不同的页面,但我不知道在主代码中将循环放在哪里而不改变主代码的结构。可以帮我。抱歉英语不好。谢谢。
答案 0 :(得分:0)
您不需要while循环来识别用户类型,因为它已经存在。您只需在下添加条件语句if($ katalaluan == $ table_katalaluan)
mysql_connect("localhost", "root","") or die(mysql_error()); //Connect to server
mysql_select_db("sistem_laporan") or die("Cannot connect to database"); //Connect to database
$query = mysql_query("SELECT * from profil WHERE id='$id'"); //Query the profil table if there are matching rows equal to $id
$exists = mysql_num_rows($query); //Checks if id exists
$table_profil = "";
$table_katalaluan = "";
if($exists > 0) //IF there are no returning rows or no existing id
{
while($row = mysql_fetch_assoc($query)) //display all rows from query
{
$table_profil = $row['id']; // the first id row is passed on to $table_profil, and so on until the query is finished
$table_katalaluan = $row['katalaluan']; // the first password row is passed on to $table_profil, and so on until the query is finished
}
if(($id == $table_profil) && ($katalaluan == $table_katalaluan)) // checks if there are any matching fields
{
if($katalaluan == $table_katalaluan)
{
$_SESSION['id'] = $id; //set the id in a session. This serves as a global variable
if($row['level']==1)
{
header('Location: home.php');
}
else if($row['level']==2)
{
header('Location: home2.php');
}
else if($row['level']==3)
{
header('Location: home3.php');
}
}
}
else
{
Print '<script>alert("Kata Laluan dimasukkan adalah salah!");</script>'; //Prompts the user
Print '<script>window.location.assign("login.php");</script>'; // redirects to login.php
}
}
else
{
Print '<script>alert("ID Pengguna dimasukkan adalah salah!");</script>'; //Prompts the user
Print '<script>window.location.assign("login.php");</script>'; // redirects to login.php
}
答案 1 :(得分:0)
我尝试@ikeMiguel代码,没有错误
你说空白页(checklogin.php) 是checklogin.php的代码吗?也许你必须从$ id变量获得价值 添加
$ id = $ _POST ['id'];
在mysql_select_db
下mysql_connect("localhost", "root","") or die(mysql_error()); //Connect to server
mysql_select_db("sistem_laporan") or die("Cannot connect to database"); //Connect database
$id = $_POST['id'];
$query = mysql_query("SELECT * from profil WHERE id='$id'"); //Query the profil table if there are matching rows equal to $id
$exists = mysql_num_rows($query); //Checks if id exists
$table_profil = "";
$table_katalaluan = "";
if($exists > 0) //IF there are no returning rows or no existing id
{
while($row = mysql_fetch_assoc($query)) //display all rows from query
{
$table_profil = $row['id']; // the first id row is passed on to $table_profil, and so on until the query is finished
$table_katalaluan = $row['katalaluan']; // the first password row is passed on to $table_profil, and so on until the query is finished
}
if(($id == $table_profil) && ($katalaluan == $table_katalaluan)) // checks if there are any matching fields
{
if($katalaluan == $table_katalaluan)
{
$_SESSION['id'] = $id; //set the id in a session. This serves as a global variable
while($row=mysql_fetch_array($query))
{
if($row['level']==1)
{
header('Location: home.php');
}
else if($row['level']==2)
{
header('Location: home2.php');
}
else if($row['level']==3)
{
header('Location: home3.php');
}
}
}
}
else
{
Print '<script>alert("Kata Laluan dimasukkan adalah salah!");</script>'; //Prompts the user
Print '<script>window.location.assign("login.php");</script>'; // redirects to login.php
}
}
else
{
Print '<script>alert("ID Pengguna dimasukkan adalah salah!");</script>'; //Prompts the user
Print '<script>window.location.assign("login.php");</script>'; // redirects to login.php
}