| |
获取表单提交的用户名和密码,通过执行查询语句来检查账号是否存在。根据查询结果进行相应的处理。
<html>
<body>
<form action="check_account.php" method="post">
<label for="username">用户名:</label><br>
<input type="text" id="username" name="username"><br>
<label for="password">密码:</label><br>
<input type="password" id="password" name="password"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
<?php
// 数据库连接信息
$servername = "localhost";
$dbusername = "your_database_username";
$dbpassword = "your_database_password";
$dbname = "your_database_name";
try {
// 创建 PDO 连接
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $dbusername, $dbpassword);
// 获取表单提交的用户名
$username = $_POST['username'];
$password = $_POST['password'];
// 构建查询语句
$stmt = $conn->prepare("SELECT * FROM users WHERE username = :username");
$stmt->execute(array(":username" => $username));
// 检查查询结果
if ($stmt->rowCount() > 0) {
header("Location: user.php"); // 存在则跳转到用户页面
} else {
echo "账号密码错误!"; // 不存在则提示错误
}
} catch (PDOException $e) {
echo "连接数据库失败: ". $e->getMessage();
}
?>
<?php
echo "欢迎回来,用户xxx";
?>
系列课程并未全部上架,处于先行测试阶段