php代码仅在输入错误的用户名和密码时显示错误

时间:2016-03-24 18:11:19

标签: php mysqli

    dbconfig.php

        <?php
        // define the credentials for connection
        define('servername','localhost');
        define('username' , 'root');
        define('password' , '');
        define('database' , 'Useradmin');
        // connecting server and databse
        $conn= mysqli_connect(servername,username,password) or die('Server not connected' .mysqli_error());
        $db = mysqli_select_db($conn,database) or die('DB not found' .mysqli_error());
        ?>

    Login.php
    <?php
          include ('config.php');
           // username and password from form

           $user=$_POST['username'];
           $pass= $_POST['password'];

           // Sql injection protection

            $user  = stripslashes($user);
            $pass  = stripslashes($pass);
            $user = mysqli_real_escape_string($conn,$user);
            $pass = mysqli_real_escape_string($conn,$pass);

        // making an sql query 

        $sql = "SELECT id FROM users WHERE  user='$user' AND pass= '$pass'";
        $query = mysqli_query($conn,$sql) or die  ('Invalid query'.mysqli_error());
        $rows = mysqli_num_rows($query) or die(mysqli_error($conn));
        if($rows==1)
        {
        header('location:mainpage.html');       
        }
        else
        echo 'Wrong password or username';
        ?>

我是PHP的新手。以上是我的代码。当我输入正确的用户名和密码并将我重定向到成功页面时它工作正常但是当用户名或密码错误时它会显示此错误

警告:mysqli_error()要求参数1为mysqli,第18行的C:\ xampp \ htdocs \ example \ login.php中给出的对象

以上是显示我看过各种答案的错误没有人有相对答案,因为只有当用户名或密码错误时才会显示错误

1 个答案:

答案 0 :(得分:0)

这是因为您没有传递MySQLI连接,而是传递查询。第18行应该是:

$rows = mysqli_num_rows($query) or die(mysqli_error($conn));