我是编码新手,目前正在努力将我的注册页面连接到 MySQL。用户名、密码、主机和数据库名称都是正确的。
代码结果:
登录页面:
<?php
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$host = "127.0.0.1";
$dbUsername = "root";
$dbPassword = "";
$dbName = "website_db";
$conn = new mysqli($host, $dbUsername, $dbPassword, $dbName);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$username = $_POST['username'];
$password = $_POST['password'];
$stmt = $conn->prepare("SELECT * FROM Users WHERE user_name = ?AND paswd = ?");
$stmt->bind_param("ss", $username, $password);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 1) {
$_SESSION['logged_in'] = true;
header("Location: main_page.php");
exit();
}
else {
echo "<p>Invalid username or password!</p>";
}
$stmt->close();
$conn->close();`
}
?>
我尝试检查 phpmyadmin
config.inc.php file
以检查用户名、密码和主机是否正确(它们是),并尝试重新安装 XAMPP 以查看是否也能正常工作。我还尝试过在 127.0.0.7
和 localhost
之间切换主机名。
我遇到了类似的问题,并且我的值周围没有引号(即 $dbPassword 应该是 '$dbPassword')
<?php
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$host = "127.0.0.1";
$dbUsername = "root";
$dbPassword = "";
$dbName = "website_db";
$conn = new mysqli('$host', '$dbUsername', '$dbPassword', '$dbName');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$username = $_POST['username'];
$password = $_POST['password'];
$stmt = $conn->prepare("SELECT * FROM Users WHERE user_name = ?AND paswd = ?");
$stmt->bind_param("ss", '$username', '$password');
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 1) {
$_SESSION['logged_in'] = true;
header("Location: main_page.php");
exit();
}
else {
echo "<p>Invalid username or password!</p>";
}
$stmt->close();
$conn->close();`
}
?>