如果有人能弄清楚这一点,那将非常有帮助。因此,我试图将错误显示在与登录表单相同的页面上。我认为问题在于表单操作,因为它总是加载该页面。我已经添加了if为空的条件和变量,但不确定如何将它们添加到HTML中。
- 错误消息已添加为php代码中显示的变量
- 目前,表单操作是一个问题(我认为),它加载带有空白页面的checklogin.php
- 如何使错误消息显示在同一页的表单下面
谢谢。。。。
HTML:
<head>
<meta charset="UTF-8">
<title>Entry form login</title>
</head>
<body>
<div class= "wrap">
<img src="/images/logo.png" alt="Highdown logo" />
<h1>Sports day</h1>
</div>
<div class='container'>
<div class='form'>
<form id ="form" action ="checklogin.php" method="post">
<label for = "user">Username:</label>
<input type ="text" name ="username"><br>
<label for ="password">Password:</label>
<input type ="password" name ="password"><br> <span class="error"> <?php echo $error;?></span>
<input type ="submit" name ="loginbutton" id ="loginbutton" value ="Log in">
</form>
</div>
</div>
<link rel="stylesheet" type="text/css" href="styles.css">
</body>
php:
<?php
session_start();
require_once 'db/connect.php';
$error='';
if (isset($_POST['loginbutton'])){
if (empty($_POST['username']) && empty($_POST['password'])) {
$error = 'Please enter your username and password';
}
else {
// Username and password sent from the form
$username = $_POST['username'];
$password = $_POST['password'];
// Protect MySQL injection
$username= stripslashes($username);
$password = stripslashes($password);
$username = mysqli_real_escape_string($con, $username);
$password = mysqli_real_escape_string($con, $password);
$encryptpass = md5($password); // Use md5 to encrypt password
$sql = "SELECT * FROM teacher WHERE Username = '$username' and Password = '$encryptpass'";
$result=mysqli_query($con, $sql);
// my_sql_num row is counting table row
$count = mysqli_num_rows($result);
//If result matched $username and $password, table row must be 1 row
if ($count==1) {
$_SESSION['Username']=$username;
header("location:homepage.php");
}
else {
$error = 'Username and/or password is invalid';
}
}
}
?>
您可以将它们组合起来,并从form元素中删除action属性。
<?php
session_start();
require_once 'db/connect.php';
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Entry form login</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<div class= "wrap">
<img src="/images/logo.png" alt="Highdown logo" />
<h1>Sports day</h1>
</div>
<div class='container'>
<div class='form'>
<form id ="form" method="post">
<label for = "user">Username:</label>
<input type ="text" name ="username"><br>
<label for ="password">Password:</label>
<input type ="password" name ="password"><br> <span class="error"> <?php echo $error;?></span>
<input type ="submit" name ="loginbutton" id ="loginbutton" value ="Log in">
</form>
<?php
$error='';
if (isset($_POST['loginbutton'])){
if (empty($_POST['username']) && empty($_POST['password'])) {
$error = 'Please enter your username and password';
}
else {
// Username and password sent from the form
$username = $_POST['username'];
$password = $_POST['password'];
// Protect MySQL injection
$username= stripslashes($username);
$password = stripslashes($password);
$username = mysqli_real_escape_string($con, $username);
$password = mysqli_real_escape_string($con, $password);
$encryptpass = md5($password); // Use md5 to encrypt password
$sql = "SELECT * FROM teacher WHERE Username = '$username' and Password = '$encryptpass'";
$result=mysqli_query($con, $sql);
// my_sql_num row is counting table row
$count = mysqli_num_rows($result);
//If result matched $username and $password, table row must be 1 row
if ($count==1) {
$_SESSION['Username']=$username;
header("location:homepage.php");
}
else {
$error = 'Username and/or password is invalid';
}
}
}
?>
</div>
</div>
</body>
</html>
另一种方法是让表单操作指向同一个脚本,如下所示:
action="<?php echo $_SERVER['PHP_SELF'] ?>"