嗨,我刚刚使用php和mysql创建了一个登录系统,登录系统在从数据库输入有效的用户名和密码时工作,但当我输入无效的用户名和密码时,它只是循环回到登录页面,即使我有声明说它失败了,我想打印如果登录失败,这里是我的代码文件
<?php
require_once("Connections/connection.php");
error_reporting(E_ALL ^ E_NOTICE);
$userid = $_POST['userid'];
$password = $_POST['password'];
$submitted = $_POST['submitted'];
if ($userid && $password){
$query =sprintf("SELECT * FROM users where user_name= '$userid' and user_password = '$password'");
$result =@mysql_query($query);
$rowAccount =@mysql_fetch_array($result);
}
if ($rowAccount){
echo "The record exists so you can enter ";
} elseif($submitted){
print "you don't exist in the system!";
}
?>
我的HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="<?php $_SERVER['PHP_SELF'];?>">
<table width="800">
<tr>
<th width="233" scope="col">User ID </th>
<th width="555" scope="col"><p>
<label for="userid"></label>
<label for="userid"></label>
<input type="text" name="userid" id="userid" />
</p>
<p> </p></th>
</tr>
<tr>
<td>Password </td>
<td> <label for="userid"></label> <label for="password"></label>
<input type="text" name="password" id="password" /> </td>
</tr>
<tr>
<td> </td>
<td> <input type="hidden" name="submitted" id="submitted" /> <input type="submit" name="Submit" id="Submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>
要使echo工作,必须向隐藏字段添加一个值,并且它工作
尝试更改
elseif($submitted){
print "you don't exist in the system!";
}
:
else(){
print "you don't exist in the system!";
}
你正在检查一个没有值的变量,因此它返回false,并且没有触发你的elseif语句。
或者,您可以将HTML部分更改为如下内容: <input type="hidden" name="submitted" id="submitted" value="test" />
你可以看到,我已经给了它一个测试值,现在只是为了让它返回true,从而触发你的elseif语句