-2

嗨,我刚刚使用 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>&nbsp; </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>&nbsp;</td>
      <td>   <input type="hidden" name="submitted" id="submitted" />        <input type="submit" name="Submit" id="Submit" value="Submit" /></td>
    </tr>
  </table>
</form>
</body>
</html>

要使回声正常工作,必须向隐藏字段添加一个值并且它起作用了

4

1 回答 1

0

尝试改变

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 语句。

于 2013-04-08T17:52:59.853 回答