2017-03-27 38 views
-4

請幫助我的代碼我不明白哪裏是事先錯誤PHP如果{} elseif的{}否則{}語法錯誤

$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

'else'不符合條件。另外,單個'='是賦值;如果您想比較兩個值是否相等,請使用'=='或'==='。 – Ryan

+0

另請參閱[The 3 different equals](http://stackoverflow.com/questions/2063480/the-3-different-equals) –

+0

你的錯誤是什麼? – chris85

回答

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"); } 
+0

thnx我使用==爲if語句 –

+0

anser幫助,這樣您可以在anser左側單擊(向上箭頭),然後單擊右側標記 –

0

您發現了所有新PHP開發人員的根本錯誤:

  • if($a=$b)分配聲明。它將$b設置爲任何正確的值。
  • if($a==$b)是比較聲明。它比較兩個值而不改變任何東西。

將您的對賬單更改爲($userrole=='…')

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或else如果。我是一名C++程序員,但我做了一點PHP。