我有一个带有字段的login.html
文件,并在其上检查表单中的值。
我的 PHP 代码位于不同的文件中form
username
password
submit button
mysql database
login.php
问题是当我输入正确的用户名和密码时它工作正常。但是,如果我输入错误的输入或将它们保留为空,那么如果重定向到login.html
页面,它会重定向到 login.php
页面。
登录.php
<?php
### Check if the Submit button is clicked
if (isset($_POST['btnSubmit']))
{
# code...
//$uname=$_POST['uname'];
//$pswd=$_POST['pswd'];
#### Connection to mySQL ####
$conn=mysqli_connect("localhost","root","","vishal") or die("Unable to connect to MySQL");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($conn,"SELECT * FROM login where username = '$_POST[uname]' AND password = '$_POST[pswd]'") or die(mysql_error());
$row = mysqli_fetch_array($result) or die(mysql_error());
$username=$row['username'];
$password=$row['password'];
if (!empty($username) AND !empty($password))
{ # code...
//$_SESSION['username'] =$password;
header('location:welcome.html');
}
else //(empty($username) AND empty($password))
{ # code...
header('location:login.html');
die();
}
}
?>
登录.html
<form action="login.php" name="login_form" method="post" enctype="multipart/form-data" target="_self">
<fieldset style="width:300px; margin:auto; margin-top:100px; border:solid 2px #900; background-color:#CCC; border-radius: 50px">
<h1 style="color:#306;"> <center> Login Form </center></h1> <br> <br>
<div style="margin-left:30px;">
<table>
<tr>
<td align="right"> <label for="uname"> <strong>Username :</strong> </label> </td>
<td> <input type="text" title="Username" id="uname" name="uname" placeholder="Username" /> </td>
</tr>
<tr><td> </td><td> </td> </tr>
<tr>
<td align="right"> <label for="pswd"> <strong>Password :</strong> </label> </td>
<td> <input type="password" title="Password" id="pswd" name="pswd" placeholder="Password" /> </td>
</tr>
<tr><td><br /> </td><td> <br /></td> </tr>
<tr>
<td></td>
<td> <input type="submit" title="Submit" name="btnSubmit" id="btnSubmit" value="Submit" style="width:100px; height:26px; font-weight:bold;" /> </td>
</tr>
<tr><td><br /> </td><td> <br /></td> </tr>
</table>
</div>
</fieldset>
</form>