2013-03-14 110 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. 并且改变你的编码风格,因为它出现了更多的性能 问题。并使用以下编码风格来减少性能问题。
相关问题