这是代码!当数据已经在数据库中时,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");
?>
答案 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");
}
}
答案 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)
include_once("include\config.php");
应为include_once("include/config.php");
查询select * from users
可能会返回大量记录。更好的查询类似于:
mysql_query(“select * from users where ulogin ='”。mysql_real_escape($ login)。“'AND”upassword ='“。$ pwd。”'“);
这应该返回行或根本不返回任何行。保存扫描整个用户表