php if {} elseif {} else {}语法错误

时间:2017-03-27 04:28:54

标签: php

请帮我处理代码我不知道错误在哪里

$userrole = $row['type'];

if ($userrole = "admin") {
        $_SESSION['name'] = $row['name'];
        header("Location: ../index.php"); 
} elseif ($userrole = "encoder") {
        $_SESSION['name'] = $row['name'];
        header("Location: ../mencode.php"); 
} elseif ($userrole = "verifier") {
        $_SESSION['name'] = $row['name'];
        header("Location: ../mverify.php"); 
} else ($userrole = "approver") {
        $_SESSION['name'] = $row['name'];
        header("Location: ../mapproved.php"); } 
提前thnx

3 个答案:

答案 0 :(得分:0)

替换你的代码

   $userrole = $row['type'];

if ($userrole == "admin") {
        $_SESSION['name'] = $row['name'];
        header("Location: ../index.php"); 
} elseif ($userrole == "encoder") {
        $_SESSION['name'] = $row['name'];
        header("Location: ../mencode.php"); 
} elseif ($userrole == "verifier") {
        $_SESSION['name'] = $row['name'];
        header("Location: ../mverify.php"); 
} elseif ($userrole == "approver") {
        $_SESSION['name'] = $row['name'];
        header("Location: ../mapproved.php"); } 

答案 1 :(得分:0)

你犯了所有新PHP开发人员的根本错误:

  • if($a=$b)作业语句。它将$b设置为右侧的任何内容。
  • if($a==$b)是一个比较声明。它在不改变任何内容的情况下比较两个值。

将您的陈述更改为($userrole=='…')

答案 2 :(得分:0)

试试这个

$userrole = $row['type'];

if ($userrole == "admin") {
        $_SESSION['name'] = $row['name'];
        header("Location: ../index.php"); 

} elseif ($userrole == "encoder") {
        $_SESSION['name'] = $row['name'];
        header("Location: ../mencode.php"); 

} elseif ($userrole == "verifier") {
        $_SESSION['name'] = $row['name'];
        header("Location: ../mverify.php"); 

} else {
        $_SESSION['name'] = $row['name'];
        header("Location: ../mapproved.php"); } 

您的if语句必须是比较运算符==而不是赋值运算符=

同样else是一个全部捕获,因此您不会像使用if或if if那样提供比较值。我是一名c ++程序员,但我做了一点PHP。