我正在构建一个登录系统。当用户点击提交时,如果输入的凭据"存在"则应输出到屏幕。或者"不存在",但屏幕上没有任何内容。这是为什么?我还有一个用phpmyadmin创建的数据库,名为" users"在我的示例数据库中。
<?php
//Database connection
define("HOST", "localhost");
define("USERNAME", "root");
define("PASSWORD", "root");
define("DATABASE", "sample");
?>
的index.php
<?php
if(version_compare(PHP_VERSION, "5.2.0", ">")){
include("config.php");
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST"){
$mysqli = new mysqli(HOST, USERNAME, PASSWORD, DATABASE);
if($mysqli->connecterrno){
echo "Please fix database connection.";
exit;
}
$prepare_stmt = $mysqli->prepare("SELECT id, username, password FROM users WHERE username = ? AND password = ?");
$prepare_stmt->bind_param("ss", $username, $password);
$username = $_POST['user'];
$password = hash("sha256", $_POST['password']);
$prepare_stmt->execute();
$prepare_stmt->store_result();
echo $password . "<br />";
if($prepare_stmt->num_rows){
echo "Exists";
} else {
echo "Doesn't Exist";
}
} else {
?><form action="" method="POST">
<input type="text" name="user"><br />
<input type="password" name="password"><br />
<button type="submit">Login</button>
</form><?php
}
}else{
?>Please update your PHP Version to suit these code settings.
<?php
}
?>
错误
警告:mysqli :: mysqli():( HY000 / 1045):拒绝访问用户&#39; root&#39; @&#39; localhost&#39; (使用密码:YES)在第8行的C:\ xampp \ htdocs \ sample \ index.php
注意:未定义的属性:第9行的C:\ xampp \ htdocs \ sample \ index.php中的mysqli :: $ connecterrno
警告:mysqli :: prepare():无法在第13行的C:\ xampp \ htdocs \ sample \ index.php中获取mysqli
致命错误:在第14行的C:\ xampp \ htdocs \ sample \ index.php中的非对象上调用成员函数bind_param()
答案 0 :(得分:3)
首先,您应该使用if($mysqli->connecterrno){
在行connect_errno
中出错。其次,您有访问被拒绝错误,这意味着您的数据库连接凭据错误。