2015-07-01 86 views
0

我有一個腳本來管理用戶登錄,並且當用戶名被輸入到字段中並且密碼被輸入到它的字段中並且它們與你登錄的數據庫相匹配,如果沒有的話返回由登錄表單解析的錯誤。然而,當你輸入一個無效的值到用戶名(不存在的用戶名)代碼不會繼續,所以我該如何解決?PHP沒有捕捉到sql錯誤

<?php 

require("bootstrap.php"); 

$con=mysql_connect(DB_HOST,DB_USER_SEC,DB_PASS_SEC) or die("Failed to connect to MySQL: " . mysql_error()); 

$db=mysql_select_db(DB_NAME_SEC,$con) or die("Failed to connect to MySQL: " . mysql_error()); 

$query = mysql_query("SELECT * FROM username WHERE userName = '$_POST[username]'") or die(mysql_error()); 

if(!empty($_POST['username'])) 
{ 
    if(!empty($_POST['password'])) 
    { 
     $row = mysql_fetch_array($query) or die(mysql_error()); 
     if(!empty($row['userName'])) 
     { 
      if(!empty($row['userPass'])) 
      { 
       if($_POST['password'] === $row['userPass']) 
       { 
        session_start(); 
        $_SESSION['logged']  = true; 
        $_SESSION['userName']  = $row['userName']; 
        $_SESSION['fname']  = $row['fname']; 
        $_SESSION['mname']  = $row['mname']; 
        $_SESSION['lname']  = $row['lname']; 
        $_SESSION['primnum']  = $row['primnum']; 
        $_SESSION['secnum']  = $row['secnum']; 
        $_SESSION['department'] = $row['department']; 
        $_SESSION['clearance'] = $row['clearance']; 
        $_SESSION['theme']  = $row['theme']; 
        $_SESSION['animations'] = $row['animations']; 
        $_SESSION['gtag']   = $row['gtag']; 

        header("Location: /workspace/index"); 
       } 
       else 
       { 
        session_start(); 

        $_SESSION['logged']  = false; 
        $_SESSION['err']   = "0x001"; 

        header("Location: /login"); 
       } 
      } 
      else 
      { 
       session_start(); 

       $_SESSION['logged']  = false; 
       $_SESSION['err']   = "0x005"; 

       header("Location: /login"); 
      } 
     } 
     else 
     { 
      session_start(); 

      $_SESSION['logged']  = false; 
      $_SESSION['err']   = "0x002"; 

      header("Location: /login"); 
     } 
    } 
    else 
    { 
     session_start(); 

     $_SESSION['logged']  = false; 
     $_SESSION['err']   = "0x003"; 

     header("Location: /login"); 
    } 
} 
else 
{ 
    session_start(); 

    $_SESSION['logged']  = false; 
    $_SESSION['err']   = "0x004"; 

    header("Location: /login"); 
} 

?> 

是的,我知道的mysqli和PDO等等請不要提起這件事。

+0

過去這個代碼在你的PHP看到錯誤'error_reporting(E_ALL); ini_set('display_errors',1);' – Shehary

+1

爲什麼你在每個'else'條件下寫'session_start();'。爲什麼不在你的頁面頂部寫'session_start();'一次。 – Saty

+0

我的代碼是要清理以後只需要解決問題我代碼段中這是醜陋的草稿 – Jdoonan

回答

1

此線代碼停止

$row = mysql_fetch_array($query) or die(mysql_error()); 

基本上mysql_fetch_array($查詢)返回false,如果沒有行去取。 所以它會掉到聲明

+0

感謝不能相信我沒有看到那部分 – Jdoonan