当数据已在数据库中时,PHP无法从数据库获取用户数据

时间:2013-03-14 11:31:59

标签: php mysql

这是代码!当数据已经在数据库中时,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");
?>

7 个答案:

答案 0 :(得分: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 :(得分: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. 并且必须检查include / config中的数据库连接。
  3. session_start()
  4. 之前没有空格
  5. 并改变您的编码风格,因为它会产生更多的性能 问题。并使用以下编码风格来减少     绩效问题。

答案 2 :(得分:0)

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

答案 3 :(得分:0)

包含您的config.php文件,如下所示:

include_once("include/config.php");

不能是这样的:

include_once("include\config.php");

答案 4 :(得分:0)

使用以下查询

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

然后你不需要一次又一次地循环所有记录。

请确保数据库表有记录。

答案 5 :(得分:0)

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

Repalce

include_once("include\config.php");

。通过

include_once("include/config.php");

答案 6 :(得分: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”upassword ='“。$ pwd。”'“);

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