登录无法找到错误

时间:2017-03-25 06:14:46

标签: php mysql mysqli html-form

您好我正在尝试使用PHP创建一个非常简单的登录功能但是,在完成代码后,我的表单似乎能够只在我输入正确的用户名和密码时执行该功能,但是,如果我不,它没有履行其职责,这是我简化的代码:

的login.php

<form name="userlogin" action="phpprocess/loginprocess.php" method="POST">

  <p>Username : <input type="text" id="username" name="username"></p>

  <p>Password : <input type="password" id="password" name="password"></p>

  <p><input type="submit" id="loginbtn" value="login" ></p>

</form>

loginproccess.php

include "mysqli.connect.php";

$username = $mysqli->real_escape_string($_REQUEST["username"]);
$password = $mysqli->real_escape_string($_REQUEST["password"]);

echo "$username"; 
echo "$password";

$sql = "select * from users.userlogin where username ='".$username."' and 
password = '".$password."'";

$result = $mysqli->query($sql);

        if($result == null){
            echo"null";
        }


        if($mysqli -> errno){
            error_log($mysqli -> error);
            echo $mysqli -> error;
            echo " hello";
            exit();

        }else{

            while( list($index, $user1, $pass1) = $result -> fetch_array()){

                if($user1 != null && $pass1 != null){
                    echo "$index $user1, $pass1";   

                }

            }

        }


$mysqli->close();

mysqli.connect.php

$host="localhost";
$user="root";
$password="";
$database="users";


$mysqli = new mysqli($host, $user, $password, $database);

if ($mysqli->errno) {
echo "Unable to connect to the database: <br />".$mysqli->error;
exit();

}

如果我输入错误的用户名和密码,我需要网页回显你好,如果我输入正确我需要它回应正确的用户并通过但是当输入错误时,$ result似乎不是null为null并且未打印出hello。我的错误日志没有显示任何内容。希望听到你的建议!提前谢谢!

1 个答案:

答案 0 :(得分:0)

当查询出错时,您打印public class InitiatePlayerCommand implements Command{ private List<Player> playerList; private String name; private WarMode warMode; private Class<?> playerClass; public <T extends Player> InitiatePlayerCommand(String name, WarMode mode ,List<Player> list ,Class<T> playerClass){ this.name = name; this.warMode = mode; this.playerList = list; this.playerClass = playerClass; } @Override public void execute() throws InstantiationException, IllegalAccessException, IllegalArgumentException, NoSuchFieldException, SecurityException { Player player = (Player) playerClass.newInstance(); player.setName(name); player.setWarMode(warMode); playerList.add(player); } public static void main(String[] argv){ List<Player> playerList = new ArrayList<Player>(); new InitiatePlayerCommand("Johnny",WarMode.MILITARY,playerList,HumanPlayer.class) .execute(); // this line get an error HumanPlayer.class isn't compatible to Class<T extends Player> playerClass System.out.println(playerList); } ,但如果hello条件与任何行不匹配,则不是错误。它只返回一个空结果集。你应该使用:

WHERE

此外,if ($result->num_rows == 0) { echo "hello"; exit(); } 返回一个包含编号索引和命名索引的数组。如果你的表有3列,这将返回一个包含6个元素的数组,但你只分配3个变量。

使用$result->fetch_array()获取仅包含编号元素的数组。此外,如果您依赖于此类元素的顺序,则不应使用$result->fetch_row(),因为这取决于在SELECT *语句中分配列的顺序,如果您在CREATE TABLE语句中分配列的顺序,则可能会更改您更新架构。您应该在SELECT列表中列出所需的特定列。