-1

这是代码!当数据已经在数据库中时,PHP 没有从数据库中获取用户数据!帮帮我,我卡住了。当我在 login.php 中输入详细信息时,它会将我重定向到 login.php?invalid=1 并显示错误信息或会话已过期!尽管信息是一样的!

<?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");
?>
4

7 回答 7

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");  
   ?>
于 2013-03-14T11:33:30.593 回答
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. 您必须检查配置文件 'include\config' 更改为 'include\config' 的文件路径。
  2. 并且必须检查包含/配置中的数据库连接。
  3. session_start() 之前没有空格
  4. 并改变你的编码风格,因为它会出现更多的性能问题。并使用以下编码风格来减少性能问题。
于 2013-03-14T12:40:15.403 回答
0

更改include_once("include\config.php");include_once("include/config.php");

于 2013-03-14T11:34:06.397 回答
0

像这样包含您的config.php文件:

include_once("include/config.php");

不可能是这样的:

include_once("include\config.php");
于 2013-03-14T11:34:17.997 回答
0

使用下面的查询

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

那么您不需要一次又一次地循环所有记录。

请确保数据库表有记录。

于 2013-03-14T11:36:55.893 回答
0

包含数据库配置文件时出现问题。

替换

include_once("include\config.php");

include_once("include/config.php");
于 2013-03-14T11:37:01.387 回答
0
  1. 您不应该使用 mysql_ 库 - 它已被弃用。请转换为 mysqli_ 或 PDO
  2. include_once("include\config.php");应该include_once("include/config.php");
  3. 该查询select * from users可能会返回大量记录。更好的查询是这样的:

    mysql_query("select * from users where ulogin = '" . mysql_real_escape($login) . "' AND "uppassword = '" . $pwd . "'");

这应该返回一行或根本不返回行。保存扫描整个用户表

于 2013-03-14T11:40:18.307 回答