当数据库中已有数据时,PHP无法从数据库中获取用户数据
这是密码!当数据库中已有数据时,PHP无法从数据库中获取用户数据!帮帮我,我卡住了。当我在login.php中输入详细信息时,它会将我重定向到login.php?invalid=1,并显示错误信息错误或会话已过期!尽管信息是一样的当数据库中已有数据时,PHP无法从数据库中获取用户数据,php,mysql,Php,Mysql,这是密码!当数据库中已有数据时,PHP无法从数据库中获取用户数据!帮帮我,我卡住了。当我在login.php中输入详细信息时,它会将我重定向到login.php?invalid=1,并显示错误信息错误或会话已过期!尽管信息是一样的 <?php session_start(); include_once("include\config.php"); $login = $_POST["textfield1"]; $pwd = $_POST["textfield
<?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");
?>
我已经改变了
发件人:
致:
及
发件人:
><?php
session_start();
$recordset = mysql_query("select * from users");
致:
请用此代码替换您的代码,然后重试。希望这对你有帮助
<?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");
?>
Change
include_once(“include\config.php”)
到包含一次(“include/config.php”)代码>包括您的config.php
文件,如下所示:
include_once("include/config.php");
include_once("include\config.php");
不可能是这样的:
include_once("include/config.php");
include_once("include\config.php");
使用下面的查询
select * from users WHERE username = "'.$login.'" AND password = "'.$pwd.'"
然后,您不需要一次又一次地循环所有记录
请确保数据库表有记录。包含数据库配置文件时出现问题。
Repalce
include_once("include\config.php");
与
include_once("include/config.php");
您不应该使用mysql库-它已被弃用。请转换为mysqli_uu或PDO
include_once(“include\config.php”)
应该是include_once(“include/config.php”)代码>
查询select*from users
可能返回大量记录。更好的查询应该是:
mysql_query(“select*from users where ulogin='”。mysql_real_escape($login)。“”和“upassword='”。$pwd.”)
这应该返回一行或不返回任何行。保存扫描整个用户表,解释您所做的更改。是的,您应该解释您所做的更改。我已经做了。很抱歉我的错误,感谢您的建议非常感谢!这很有帮助:)感谢您的回答!如果我的回答对您有帮助,那么请正确回答,这样它就会正确对其他用户很有帮助,因为您似乎以纯文本形式保存密码。请不要这样做。另外请注意,mysql\uu
函数已被弃用,不应再使用。请改用PDO或MySQLi。
<?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");
}
}
include_once("include/config.php");
<?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");
}
}