首页 > 解决方案 > 无法在 PHP 站点中检索 MySQL 数据

问题描述

我有这个代码,登录页面将调用它来运行这个 php 脚本。但是,代码一直告诉我用户名和密码不正确。因此,我添加了回声以查看它从数据库中获得了什么值。显然,查询返回用户名和密码的“”。

我在数据库中只有一条名为“appthub_db”的记录,名为“tbl_user”的表,表列“用户名”和“密码”。

我没有收到连接错误(我认为)。我不知道出了什么问题?

<!DOCTYPE HTML>
<html>
<body>
<?php 
include_once("DBConnection.php"); 
session_start(); //always start a session in the beginning 
if ($_SERVER['REQUEST_METHOD'] == 'POST') 
{ 
if (empty($_POST['username']) || empty($_POST['password'])) //Validating inputs using PHP code 
{ 
echo 
"Incorrect username or password"; //
header("location: LoginForm.php");//You will be sent to Login.php for re-login 
} 
$inUsername = $_POST["username"]; // as the method type in the form is "post" we are using $_POST otherwise it would be $_GET[] 
$inPassword = $_POST["password"]; 

echo $inUsername;
echo nl2br ("\n ");
echo $inPassword; 
echo nl2br ("\n ");

$stmt= $db->prepare("SELECT username, password FROM tbl_user WHERE username = ?"); //Fetching all the records with input credentials
$stmt->bind_param("s", $inUsername); //bind_param() - Binds variables to a prepared statement as parameters. "s" indicates the type of the parameter.
$stmt->execute();

$stmt->bind_result($UsernameDB, $PasswordDB); // Binding i.e. mapping database results to new variables
//Compare if the database has username and password entered by the user. Password has to be decrypted while comparing.

echo "DBUsername:" ;
echo $UsernameDB ;
echo nl2br ("\n ");
echo "DBPassword:" ;
echo $PasswordDB;

if ($stmt->fetch() && password_verify($inPassword, $PasswordDB)) 
{
$_SESSION['username']=$inUsername; //Storing the username value in session variable so that it can be retrieved on other pages
header("location: UserProfile.php"); // user will be taken to profile page
}
else
{
echo "Incorrect username or password";
?><br>
<a href="LoginForm.php">Login</a>
<?php 
} 
} 
?>
</body> 
</html>

标签: phpmysql

解决方案


推荐阅读