2013-03-14 107 views
-1

這是代碼!當數據已經在數據庫中時,PHP沒有從數據庫獲取用戶數據!幫助我,我卡住了。當我在login.php中輸入詳細信息時,它會將我重定向到login.php?invalid = 1,並顯示錯誤信息或會話過期錯誤!儘管信息是一樣的!當數據已經在數據庫中時,PHP沒有從數據庫中獲取用戶數據

<?php 
    session_start(); 
    include_once("include\config.php"); 
    $login = $_POST["textfield1"]; 
    $pwd = $_POST["textfield2"]; 
    $recordset = mysql_query("select * from users"); 
    while($record = mysql_fetch_array($recordset)){ 
    if($login == $record["ulogin"] && $pwd == $record["upassword"]) { 
     $_SESSION["ulogin"] = $record["ulogin"]; 
     $_SESSION["uid"] = $record["uid"]; 
     if($record["utype"] == 1){ 
       $_SESSION["utype"] = $record["utype"]; 
       header("Location:admin.php?uid=".$record["uid"]); 
       exit; 
     }else{ 
       header("Location:home.php"); 
       exit; 
      } 
     } 
    } 
    header("Location:login.php?invalid=1"); 
?> 
+1

您似乎以純文本格式保存密碼。請不要這樣做。另請注意,'mysql_'函數已被棄用,不應再使用。改爲使用PDO或MySQLi。 – Arjan 2013-03-14 11:41:07

回答

1

我已經改變 來源:

include_once("include\config.php"); 

要:

include_once("include/config.php"); 

來源:

><?php 
session_start(); 

要:

<?php 
    session_start(); 

來源:

$recordset = mysql_query("select * from users"); 

要:

$recordset = mysql_query("select * from users WHERE username = "'.$login.'" AND password = "'.$pwd.'"); 

請使用此代碼替換您的代碼和嘗試。希望這會幫助你。

<?php 
session_start(); 
include_once("include/config.php"); 
$login = $_POST["textfield1"]; 
$pwd = $_POST["textfield2"]; 
$recordset = mysql_query("select * from users WHERE username = "'.$login.'" AND password = "'.$pwd.'""); 
while($record = mysql_fetch_array($recordset)){ 
if($login == $record["ulogin"] && $pwd == $record["upassword"]) { 

$_SESSION["ulogin"] = $record["ulogin"]; 
$_SESSION["uid"] = $record["uid"]; 
     if($record["utype"] == 1){ 
     $_SESSION["utype"] = $record["utype"]; 
     header("Location:admin.php?uid=".$record["uid"]); 
     exit; 
     }else{ 
    header("Location:home.php"); 
    exit; 
    } 
} 
} 

     header("Location:login.php?invalid=1"); 
    ?> 
+1

解釋你有什麼改變。 – 2013-03-14 11:34:54

+0

是的,你應該解釋你所做的改變。 – 2013-03-14 11:35:24

+0

我已經完成了。對不起,我的錯誤,並感謝您的建議 – Hardik 2013-03-14 11:38:54

0

變化include_once("include\config.php");include_once("include/config.php");

0

包括您config.php文件是這樣的:

include_once("include/config.php"); 

它不能是這樣的:

include_once("include\config.php"); 
0

使用下面的查詢

select * from users WHERE username = "'.$login.'" AND password = "'.$pwd.'" 

然後您不需要一遍又一遍循環所有記錄。

請確保數據庫表有記錄。

0

包含數據庫配置文件時出現問題。

Repalce

include_once("include\config.php"); 

隨着

include_once("include/config.php"); 
0
  1. 你不應該使用mysql_庫 - 它已經過時了。請轉換要麼mysqli_或PDO
  2. include_once("include\config.php");應該include_once("include/config.php");
  3. 查詢select * from users是可能返回的記錄數休。一個更好的查詢會是這樣的:

    的mysql_query;

  4. ( 「SELECT * FROM用戶那裏ulogin =「」「和 「upassword = '」 $ PWD。 「'」 mysql_real_escape($登錄)。')

這應該返回一個行或沒有行的。保存掃描整個用戶表

1
<?php 
    session_start(); 
    // http://stackoverflow.com/questions/15408037/php-not-getting-user-data-from-database-when-data-is-already-in-database 
    include_once("include/config.php"); 
    $login = $_POST["textfield1"]; 
    $pwd = $_POST["textfield2"]; 
    $recordset = mysql_query("select * from users WHERE ulogin = "'.$login.'" AND upassword = "'.$pwd.'"" "); 
    $num_row_user =mysql_num_rows($recordset); 
    if($num_row_user>0) 
    { 
     while($record = mysql_fetch_array($recordset)) 
     { 
     if($record["utype"] == 1) 
     { 
      $_SESSION["ulogin"] = $record["ulogin"]; 
      $_SESSION["uid"] = $record["uid"]; 
      $_SESSION["utype"] = $record["utype"]; 
      header("Location:admin.php?uid=".$record["uid"]); 
      exit; 
     } 
     else 
     { 
      header("Location:home.php"); 
      exit; 
     } 
     } 
    } 
    else 
    { 
     if (mysql_errno()) 
     { 
      header("Location:login.php?invalid=1&message=QueryError"); 
     } 
     else 
     { 
      header("Location:login.php?invalid=1&message=invalid username and password"); 
     } 

    } 
  1. 你必須檢查配置文件的文件路徑「包括\ CONFIG」 改變'include \ config'。
  2. 並且必須在include/config中檢查你的db連接。
  3. 在session_start()之前沒有空格
  4. 並且改變你的編碼風格,因爲它出現了更多的性能 問題。並使用以下編碼風格來減少性能問題。
相關問題