gpt4 book ai didi

php - HTML/PHP/SQL 登录页面问题

转载 作者:行者123 更新时间:2023-11-30 01:01:32 25 4
gpt4 key购买 nike

花了不少时间弄清楚为什么我的代码不起作用。基本上,用户名是big,密码是foot,点击提交后,会查询数据库,一旦验证,页面就会重定向到我的index.php。然而,这并没有发生。事实上,当输入此用户名和密码时,isset() 甚至没有检测到提交按钮。然而,如果您输入任何其他用户名或密码,isset() 会在表单提交时进行处理。奇怪吗?可以解决吗?你告诉我!这是我的代码:

<?php
session_start();
include_once '../dbConn.php';
if (isset($_POST['submitbtn'])) {
echo "submit button detected...<br />";
echo '<script>alert("this is working...");</script>';
$sql = "SELECT * FROM flubberFitProfiles WHERE username=:username AND password=:password";
$stmt = $dbConn->prepare ( $sql );
$stmt->execute ( array (
":username" => $_POST ['name'],
":password" => hash ( "sha1", $_POST ['pass'] )
) );
$record = $stmt->fetch ();
echo "record has been fetched...<br />";
}

if (!empty($record)){
$_SESSION ['username'] = $record ['username'];
$_SESSION ['adminName'] = $record ['firstName'] . " " . $record ['lastName'];
header ( "Location: index.php" );
}
else if (empty($record)){
$errorArray = array (
"Wrong username/password"
);
}
?>
<!DOCTYPE HTML>
<HTML>
<HEAD>
<META charset="utf-8">
<TITLE>Please Log In</TITLE>
</HEAD>
<BODY>
<H1>Please log in to update your profile:</H1>
<BR />
<BR />
<form method="post" action="login.php">
Username: <INPUT type="text" id="name" name="name" required /><BR />
Password: <INPUT type="password" id="pass" name="pass" required /><BR />
<INPUT type="submit" value="Submit" id="submitbtn" name="submitbtn" />
</form>
<br />
<br />
<!--
To test the features, use:<br />
username: big<br />
password: foot<br />
-->
</BODY>
</HTML>

最佳答案

想通了...花了我几个小时,但总是一些简单的事情让我绊倒!事实证明,我只是忘记在该重定向到的页面上包含 session_start() 命令。如果没有这个,我的index.php页面将永远不会知道我是否成功登录并继续重定向回login.php页面。现在可以完美运行了!

关于php - HTML/PHP/SQL 登录页面问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20085709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com