Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP中的用户访问级别_Php_Mysql_Xampp_Dreamweaver - Fatal编程技术网

PHP中的用户访问级别

PHP中的用户访问级别,php,mysql,xampp,dreamweaver,Php,Mysql,Xampp,Dreamweaver,我的问题对你来说可能只是一个简单的问题,但我仍然不明白把这个循环代码放在哪里 我应该把这个放在哪里: while($row=mysql_fetch_array($query)) { if($row['level']==1) { header('Location: home.php'); } else if($row['level']==2) { header('Location: home2.php'); }

我的问题对你来说可能只是一个简单的问题,但我仍然不明白把这个循环代码放在哪里

我应该把这个放在哪里:

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)到不同的页面,但我不知道在不改变主代码结构的情况下在主代码中放置while循环。希望有人能帮助我。抱歉,英语不好。谢谢。

您不需要while循环来标识用户类型,因为它已经存在。只需在if($katalaluan==$table_katalaluan)下添加条件语句

mysql\u connect(“localhost”、“root”或“”)或die(mysql\u error())//连接到服务器
mysql_select_db(“sistem_laporan”)或die(“无法连接到数据库”)//连接到数据库
$query=mysql_query(“从profile中选择*,其中id='$id')//如果存在等于$id的匹配行,则查询profil表
$exists=mysql\u num\u行($query)//检查id是否存在
$table_profil=“”;
$table_katalaluan=“”;
if($exists>0)//如果没有返回行或没有现有id
{
while($row=mysql\u fetch\u assoc($query))//显示查询中的所有行
{
$table_profil=$row['id'];//第一个id行将传递给$table_profil,依此类推,直到查询完成
$table_katalaluan=$row['katalaluan'];//第一个密码行将传递给$table_profil,依此类推,直到查询完成
}
if($id=$table_profil)&&($katalaluan==$table_katalaluan))//检查是否有任何匹配字段
{
if($katalaluan==$table_katalaluan)
{
$\u SESSION['id']=$id;//在会话中设置id。此id用作全局变量
如果($row['level']==1)
{
标题('Location:home.php');
}
else if($row['level']==2)
{
标题('Location:home2.php');
}
else if($row['level']==3)
{
标题('Location:home3.php');
}
}
}
其他的
{
打印“警报”(“Kata Laluan dimasukkan adalah salah!”);//提示用户
打印“window.location.assign”(“login.php”);;//重定向到login.php
}
}
其他的
{
打印“警报”(“ID Pengguna dimasukkan adalah salah!”);//提示用户
打印“window.location.assign”(“login.php”);;//重定向到login.php
}

我尝试了@ikeMiguel代码,但没有得到错误

你说有空的空白页面(checklogin.php) 这是checklogin.php的代码吗

也许您必须从$id变量中获取值 加

$id=$\u POST['id']

在mysql_下选择_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
     }
mysql\u connect(“localhost”、“root”或“”)或die(mysql\u error())//连接到服务器
mysql_select_db(“sistem_laporan”)或die(“无法连接到数据库”)//连接数据库
$id=$_POST['id'];
$query=mysql_query(“从profile中选择*,其中id='$id')//如果存在等于$id的匹配行,则查询profil表
$exists=mysql\u num\u行($query)//检查id是否存在
$table_profil=“”;
$table_katalaluan=“”;
if($exists>0)//如果没有返回行或没有现有id
{
while($row=mysql\u fetch\u assoc($query))//显示查询中的所有行
{
$table_profil=$row['id'];//第一个id行将传递给$table_profil,依此类推,直到查询完成
$table_katalaluan=$row['katalaluan'];//第一个密码行将传递给$table_profil,依此类推,直到查询完成
}
if($id=$table_profil)&&($katalaluan==$table_katalaluan))//检查是否有任何匹配字段
{
if($katalaluan==$table_katalaluan)
{
$\u SESSION['id']=$id;//在会话中设置id。此id用作全局变量
while($row=mysql\u fetch\u array($query))
{
如果($row['level']==1)
{
标题('Location:home.php');
}
else if($row['level']==2)
{
标题('Location:home2.php');
}
else if($row['level']==3)
{ 
标题('Location:home3.php');
}
}
}
}
其他的
{
打印“警报”(“Kata Laluan dimasukkan adalah salah!”);//提示用户
打印“window.location.assign”(“login.php”);;//重定向到login.php
}
}
其他的
{
打印“警报”(“ID Pengguna dimasukkan adalah salah!”);//提示用户
打印“window.location.assign”(“login.php”);;//重定向到login.php
}

您的代码已经包含一个在结果上循环的循环,因此我将把循环的内容(而不是循环本身)放在现有的循环中。还有一件事,您应该查看mysqli或PDO的db连接,并查看准备好的语句,它们更安全。@furrie我试图在($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');}}在header('Location:home.php')处);在main中,但它重定向到空空白页您确定级别字段包含1到3之间的值吗?并且在当前代码之前您还没有完成一些输出?@furrie im sure..有一个表单要求管理员输入级别..下拉菜单中只有2和3..级别1是管理员的,我手动插入mysqli中刚刚尝试了code您已更正,但它重定向到空空白页(checklogin.php)@hicarl,而不是仅使用home.php,请尝试使用完整的url。我在代码顶部编辑了会话部分。请随时再次查看代码。有关您的信息,请参阅此页之前的checklogin.php
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
     }