dongzha5934 2018-07-10 13:00
浏览 192

使用PHP和MYSQL登录页​​面基于用户角色

I created a login page.

I want customer and manager to login from this same page. I separated customer and manager in db by their role "","manager" respectively (customer represented as null value).

I wrote this code but its not working as i expected. When I type name and password and press login button it doesn't go to desired page rather it remains on login page.

<?php
session_start();

$db=mysqli_connect("localhost","root","","authentication");

if (isset($_POST['login_btn'])) {

    $customer_name = mysql_real_escape_string($_POST['customer_name']);
    $password = mysql_real_escape_string($_POST['password']);
    $password=md5($password);
    if(empty($customer_name)&&empty($password)) {
        $error="Fields are Mandatory";
    } else {
        $sql="SELECT * 
              FROM customers 
              WHERE customer_name='$customer_name' AND password='$password'";
        $result=mysqli_query($db,$sql);
        $row=mysqli_fetch_array($result);
        $_SESSION['id']=$row['id'];
        $_SESSION['role']=$row['role'];
        $count=mysqli_num_rows($result);

        if($count==1){
            if ($row['role']=="manager"){
                header("location:manager.php");      
            }
            else if($row['role']==""){
                header("location:ueser.php");      
            }
        }else{
            $error='Customer_name/password combination incorrect';
        }
    }
}
?>
  • 写回答

2条回答 默认 最新

  • doucheng3811 2018-07-10 13:07
    关注

    Use elseif instead else if

    /* Incorrect Method: */
    if ($a > $b):
        echo $a." is greater than ".$b;
    else if ($a == $b): // Will not compile.
        echo "The above line causes a parse error.";
    endif;
    
    
    /* Correct Method: */
    if ($a > $b):
        echo $a." is greater than ".$b;
    elseif ($a == $b): // Note the combination of the words.
        echo $a." equals ".$b;
    else:
        echo $a." is neither greater than or equal to ".$b;
    endif;
    

    For More http://php.net/manual/en/control-structures.elseif.php#control-structures.elseif

    评论

报告相同问题?

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗