我的登录表单有一点问题。我认为问题在于 mysqli 代码,因为当代码是旧的 mysql 代码时,登录可以正常工作,但是自从我将其更改为 mysqli 后,它就不能正常工作了。
假设发生的是,用户将在登录表单中输入他们的用户名和密码,当用户单击“登录”按钮时,它将检查用户名和密码是否正确。如果正确,则导航到 menu.php 页面,否则如果登录不正确,则显示一条消息,说明登录不正确,重试。
相反,下面的代码所做的是,当用户输入他们的用户名和密码并单击“登录”按钮时,无论用户名和密码是否正确,它都会刷新来自,它不会导航到菜单。 php 页面或显示登录错误消息。
所以我的问题是为什么会发生这种情况,为什么登录后它不导航用户或显示不正确的登录消息?
下面是代码:
<?php
session_start();
$username="xxx";
$password="xxx";
$database="xxx";
$mysqli = new mysqli("localhost", $username, $password, $database)or die( "Unable to select database");
foreach (array('teacherusername','teacherpassword') as $varname) {
$$varname = (isset($_POST[$varname])) ? $_POST[$varname] : '';
}
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" id="teachLoginForm">
<p>Username</p><p><input type="text" name="teacherusername" /></p> <!-- Enter Teacher Username-->
<p>Password</p><p><input type="password" name="teacherpassword" /></p> <!-- Enter Teacher Password-->
<p><input id="loginSubmit" type="submit" value="Login" name="submit" /></p>
</form>
<?php
if (isset($_POST['submit'])) {
$query = $mysqli->prepare("
SELECT * FROM Teacher t
WHERE
(t.TeacherUsername = '".mysqli_real_escape_string($teacherusername)."')
AND
(t.TeacherPassword = '".mysqli_real_escape_string($teacherpassword)."')
");
$query->bind_result($Teacher);
$num = $query->num_rows($result = $query->execute());
$loged = false;
while($row = $result->fetch())
{
if ($_POST['teacherusername'] == ($row['TeacherUsername']) && $_POST['teacherpassword'] == ($row['TeacherPassword']))
{
$loged = true;
}
$_SESSION['teacherforename'] = $row['TeacherForename'];
$_SESSION['teachersurname'] = $row['TeacherSurname'];
$_SESSION['teacherusername'] = $row['TeacherUsername'];
}
if ($loged == true){
header( 'Location: menu.php' ) ;
}else{
echo "The Username or Password that you Entered is not Valid. Try Entering it Again.";
}
}
?>