我的登录表单有一点问题。我认为问题在于 mysqli 代码,因为当代码是旧的 mysql 代码时,登录可以正常工作,但是自从我将其更改为 mysqli 后,它就不能正常工作了。
假设发生的是用户将在登录表单中输入他们的用户名和密码,当用户单击“登录”按钮时,如果用户名和密码正确,它将检查数据库。如果正确,则导航到 menu.php 页面,否则如果登录不正确,则显示一条消息,指出登录不正确,然后重试。
相反,下面的代码正在做的是,当用户输入他们的用户名和密码并单击“登录”按钮时,无论用户名和密码是否正确,它都会刷新来自,它不会导航到菜单。 php 页面或显示登录错误消息。
所以我的问题是为什么会发生这种情况,为什么登录后它不导航用户或显示不正确的登录消息?
代码已更新以显示当前代码:
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
// PHP code
session_start();
$username="xxx";
$password="xxx";
$database="mobile_app";
$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] : '';
}
// move this outside the condiitonal
$loged = false;
if (isset($_POST['submit'])) {
$query = $mysqli->prepare("
SELECT * FROM Teacher t
WHERE
(t.TeacherUsername=?)
AND
(t.TeacherPassword=?)
");
$stmt=$mysqli->prepare($query);
$stmt->bind_param("s",$teacherusername);
$stmt->bind_param("s",$teacherpassword);
$stmt->execute();
$stmt->bind_result($TeacherId,$TeacherForename,$TeacherSurname,$TeacherUsername,$TeacherPassword);
while($row=$stmt->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' ) ;
}
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Teacher Login </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="LoginStyle.css">
</head>
<body>
<?php if ($loged == false && $_POST) {
echo "The Username or Password that you Entered is not Valid. Try Entering it Again.";
}
?>
<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>
</body>
</html>
更新:
我已打开错误报告,并使用从其中一个答案中检索到的上述代码,它给了我一个警告和一个致命错误,如下所示:
Warning: mysqli::prepare() expects parameter 1 to be string, object given in /web/stud/xxx/Mobile_app/teacherlogin.php on line 33
Fatal error: Call to a member function bind_param() on a non-object in /web/stud/xxx/Mobile_app/teacherlogin.php on line 34
这些是什么意思,有没有人知道如何解决它们?我更新了上面的代码以显示当前代码。