2015-04-05 37 views
0
<?php 
    session_start(); 
    include("connection.php"); 
    if(isset($_GET['submit'])) { 
     mysql_connect('localhost','root','') or die(mysql_error()); 
     mysql_select_db('awnb') or die(mysql_erroe()); 
     $Email=$_GET['LoginEmail']; 
     $password=$_GET['LoginPassword']; 
     if($Email!=""&&$password!="") { 
      $query=mysql_query("select * from users where Email='".$Email."' and Password='".$password."'") or die(mysql_error()); 
      $res=mysql_fetch_row($query); 
      if($res) { 
       $userType = $res['Type']; 
       if($userType == 'user') { 
        $_SESSION['Email']=$Email; 
        header("location:profileuser.php"); 
        exit; 
       } else if($userType == 'sec') { 
        $_SESSION['Email']=$Email; 
        header("location:profile.php"); 
        exit; 
       } 
      } else { 
       echo " 
       <script type='text/javascript'> 
       alert('Username or Password is incorrect'); 
       </script>"; 
       header("location:index.php"); 
      } 
     } else { 
      echo " 
      <script type='text/javascript'> 
      alert('Enter both Username and Passowrd'); 
      </script>"; 
      header("location:index.php"); 
     } 
    } 
?> 
  • 在這種類型的代碼是在表中的一個屬性,它講述了用戶的類型,有兩種類型的用戶稱爲用戶或仲的。如果類型是用戶,它應該重定向到profileuser.php否則到profile.php,但我無法根據用戶類型重定向頁面。
+0

當你嘗試重定向時,你得到的錯誤是什麼? – 2015-04-05 17:01:13

+0

注意:未定義的索引:輸入第16行的C:\ xampp \ htdocs \ login_phpform.php – Harindranath 2015-04-05 17:02:47

+3

@Harindranath:然後數據庫的結果中沒有「Type」列。 – David 2015-04-05 17:04:02

回答

3

要回答你的問題,你所擷取具有numeric index行:

和mysql_fetch_row()從指定結果標識符相關 結果獲取數據的一行。該行作爲數組返回。 每個結果列存儲陣列偏移offset開始,0

如果你想獲得一個關聯數組,你需要mysql_fetch_assoc

這就是說,有這麼多你的代碼錯誤,你或許應該重新開始:

  • mysql_*功能已被棄用;
  • 您有SQL injection問題;
  • 您正在存儲純文本密碼;
  • 您正在呼應header重定向之前的輸出;
  • 您使用GET而不是POST發送敏感登錄數據。

你可以在這裏找到很多有關每個點的信息,所以我不打算重複它,但就像我說的,你應該重新開始。

+0

謝謝你,先生,它的工作。 – Harindranath 2015-04-05 17:18:17

+0

@Hindindranath請參閱http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work然後返回到答案並執行相同的操作,以便正確地關閉問題並將其標記爲解決了。 ;-) – 2015-04-05 17:23:48

+0

@ Fred-ii-感謝您的編輯。我實際上猶豫了是否應該回答,因爲這對週日下午來說太過分了:-) – jeroen 2015-04-05 17:26:03