php - 将我的 SQL 数据库与 XAMPP 连接时出现问题
问题描述
我正在尝试使用我的 Google Chrome 扩展程序连接到我的 SQL 数据库。我收到以下错误:
(您的文件未找到它可能已被移动或删除。ERR_FILE_NOT_FOUND)
当我在 HTML 文件上单击登录时出现,而不是我试图用 PHP 实现的输出消息
(登录页面.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css?family=Lato:100,300,400,700,900" rel="stylesheet">
<link rel="stylesheet" href="/CSS/Style.css">
<link rel="stylesheet" href="css/icon-font.css">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Elite Bug Tracker</title>
</head>
<body>
<div class="container">
<div class="Content">
<div class="Column1">
<img src="img/bug.png" class="bug">
<h2>ELITE BUG TRACKER</h2>
</div>
<div class="Column2">
<h1>Login Here</h1>
<form action="login.php" method="POST">
<div class="login">
<p>USERNAME</p>
<input type="text" name="username" placeholder="Enter Email" id="username" required/>
<p>PASSWORD</p>
<input type="password" name="password" placeholder="Enter Password" id="password" required />
</div>
<div class="Enter">
<button type="Submit" value="login">Login</button>
</div>
<div class="Links">
<ul>
<li><a href="#">Log in as demo user</a></li>
<li><a href="#">Forgot password</a></li>
<li><a href="#">Register as new user</a></li>
</ul>
</div>
</form></div>
</div>
</div>
</body>
</html>
(登录.php)
<?php
session_start();
// Change this to your connection info.
$DATABASE_HOST = 'localhost';
$DATABASE_USER = 'root';
$DATABASE_PASS = '';
$DATABASE_NAME = 'Bug Tracker App';
// Try and connect using the info above.
$con = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
if (mysqli_connect_error()) {
// If there is an error with the connection, stop the script and display the error.
exit('Failed to connect to MySQL: ' . mysqli_connect_error());
}
// Now we check if the data from the login form was submitted, isset() will check if the data exists.
if (!isset($_POST['username'], $_POST['password'])) {
// Could not get the data that should have been sent.
exit('Please fill both the username and password field!');
}
// Prepare our SQL, preparing the SQL statement will prevent SQL injection.
if ($stmt = $con->prepare('SELECT id, password FROM accounts WHERE username = ?')) {
// Bind parameters (s = string, i = int, b = blob, etc), in our case the username is a string so we use "s"
$stmt->bind_param('s', $_POST['username']);
$stmt->execute();
// Store the result so we can check if the account exists in the database.
$stmt->store_result();
// If the username exiusts
if ($stmt->num_rows > 0) {
$stmt->bind_result($id, $password);
$stmt->fetch();
// Account exists, now we verify the password.
// Note: remember to use password_hash in your registration file to store the hashed passwords.
if (password_verify($_POST['password'], $password)) {
// Verification success! User has loggedin!
// Create sessions so we know the user is logged in, they basically act like cookies but remember the data on the server.
session_regenerate_id();
$_SESSION['loggedin'] = TRUE;
$_SESSION['name'] = $_POST['username'];
$_SESSION['id'] = $id;
header('Location: home.php');
} else {
echo 'Incorrect password!';
}
} else {
echo 'Incorrect username!';
}
} else {
echo 'Could not prepare statement!';
}
?>
解决方案
推荐阅读
- python - 计算 xarray 数据集中两个变量的外积
- python - python 太多值无法解压 xlsxwriter
- php - PHP - 获取并计算 stdClass 中的所有内容
- java - 在 Jhipster 中创建具有 ROLE_ADMIN 权限的 @Scheduled 任务
- java - 用于将半透明图像绘制到另一个图像上的算法
- ios - PWA 闪屏变黑?
- javascript - 在父类javascript中选择标签
- windows - 远程系统服务重启
- amazon-web-services - ElasticBeanstalk `eb setenv` - 应用程序重启
- xamarin - 你如何正确地继承 MvxCollectionViewSource?