php - 无法在 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>
解决方案
推荐阅读
- reactjs - Redux 操作在 mapStateToProps 函数中抛出“TypeError:无法读取未定义的属性 'num_of_players'”
- f# - F#如何为主从数据构造一个Linq查询?
- character - 如何在 user.js 文件中键入看起来像汉堡菜单的三行等号?
- python - 如何将多索引 pandas DataFrame 写入 docx 文件
- javascript - JS中如何将混合类型的嵌套子对象转换为数组?
- design-patterns - 如何在 Elixir 中的进程之间共享状态?
- c++ - Msys2 编译 C++ 太慢
- python - 使用 Python 提取交互式地图数据
- r - R - NA 提交 mann-whitney u 测试
- javascript - 向上滚动时如何将一个div隐藏在另一个半透明的div后面