我是网络开发的新手,我尝试使用PHP进行登录。 mylogin.php的内容在加载文件index.php之后立即得到处理,我在这里有点困惑。下面的截图是加载索引文件后的响应。
response just after loading the index.php without doing anything
的index.php
<!DOCTYPE html>
<html>
<head>
<title>
Welcome
</title>
</head>
<body>
<h1> Please login</h1>
<br>
<!--
<form action="" method="POST">
<label>Username: </label><br>
<input type="text" name="username" placeholder="Enter username"/><br>
<label>Password: </label><br>
<input type="password" name="password" placeholder="Enter password"/><br><br>
<input type="submit" name="submit" value="Login"/><br><br>
</form>
-->
<form method="post" action="">
<label>Username:</label><br>
<input type="text" name="username" placeholder="username" /><br><br>
<label>Password:</label><br>
<input type="password" name="password" placeholder="password" /> <br><br>
<input type="submit" name="submit" value="Login" />
</form>
</body>
</html>
<?php
include("mylogin.php");
?>
mylogin.php
<?php
include("myconnection.php");
$error="";
if(empty($_POST["submit"]))
{
if($_POST["username"] =='' || $_POST["password"]=='')
{
$error='Please fill the blanks!';
}else
{
$username=$_POST['username'];
$password=$_POST['password'];
$sql="SELECT * FROM users WHERE username='$username' AND password='$password'";
$result=mysqli_query($db,$sql);
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
if(mysqli_num_rows($result)==1)
{
$login_user=$_POST["$username"];
header("location: myhome.php");
$error="Connected";
}
else
{
//$error="Incorrect Username/Password";
// echo"failed";
}
}
}
else
{
echo"failed to submit2";
}
?>
答案 0 :(得分:2)
您遇到这种情况if(empty($_POST["submit"]))
时出现问题。你想要这个的负面条件,所以你需要像:
if(!empty($_POST["submit"]))
(!
)
答案 1 :(得分:1)
更改包含mylogin.php
文件的方式,如下面的代码所示:
并且不存在这个问题。
欢迎
<h1> Please login</h1>
<br>
<!--
<form action="" method="POST">
<label>Username: </label><br>
<input type="text" name="username" placeholder="Enter username"/><br>
<label>Password: </label><br>
<input type="password" name="password" placeholder="Enter password"/><br><br>
<input type="submit" name="submit" value="Login"/><br><br>
</form>
-->
<form method="post" action="">
<label>Username:</label><br>
<input type="text" name="username" placeholder="username" /><br><br>
<label>Password:</label><br>
<input type="password" name="password" placeholder="password" /> <br><br>
<input type="submit" name="submit" value="Login" />
</form>
</body>
</html>
<?php
if(isset($_POST["submit"]))
{
include("mylogin.php");
}
?>
当然,更改if
文件的mylogin.php
条件,如下所示:
if(isset($_POST["submit"])) OR you can change it
if(!empty($_POST["submit"]))