如果有人能够弄明白这一点,那会非常有帮助。所以我试图在登录表单的同一页面上显示错误。我认为问题是表单操作,因为总是加载该页面。我添加了空条件和变量但不确定如何将它们添加到HTML中。
...谢谢
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';
}
}
}
?>
答案 0 :(得分:1)
您可以组合它们并从表单元素中删除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>
答案 1 :(得分:0)
另一种方法是让表单操作指向同一个脚本,如下所示:
action="<?php echo $_SERVER['PHP_SELF'] ?>"