登录后显示错误

时间:2017-06-12 06:53:30

标签: php html mysql

的index.php

    <?php
    include 'login.php';
    if(isset($_SESSION['login_user']) && $_SESSION['userlevel'] == 1)
    header("location: profile.php");
    elseif(isset($_SESSION['login_user']) && $_SESSION['userlevel']==2)
    header("location: profile1.php");
    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <title>Login Form </title>
        <link href = "style.css" rel = "stylesheet" type ="text/css">
    </head>
    <body background ="water.jpg">
        <div id = "main" align = "center">
        <table align = "center" width ="100%">
            <tr>
            <td><img src= "asset2.jpg" width ="200" height ="156"></td>
            <td><img src= "asset3.jpg" width ="500" height ="156"></td>
                <td><img src= "asset2.jpg" width ="200" height ="156"></td>
            </tr>
            </table>
            <table align = "center" bgcolor = "#FF00CC" width = "100%" height ="5">
            <tr>
                <td>
                <marquee><font color= "#FFFFFF" face = "22eorgia"><b><i>HELLO..PLEASE FILL IN THE DETAILS TO LOGIN..</i></b></font></marquee>
                </td>
            </tr>
            </table>
            <div id = "login">
            <h2 align = "left">Login Form</h2>
            <form action = "" method = "post">
                <br/>
                <br/>
                <label>
                <div align = "left">UserName:
                </div>
                </label>
                <div align = "left">
                <input id = "name" name ="username" placeholder = "Enter the username" type = "text">
                </div>
                <label>
                <div align = "left">Password:
                </div>
                </label>
                <div align = "left">
                <input type ="password" name = "password" placeholder="Enter the password" type = "password">
                <br/>
                <br/>
                <input name = "submit" type = "submit" value = "Login">
                <span><?php echo $error;?></span>
                </div>
            </form>
            </div>
        </div>
        </body>
    </form>

的login.php

    <?php
    session_start();
    $error="";
    if(isset($_POST['submit']))
    {
      if(empty($_POST['username']) || empty($_POST['password'])){
        $error = "Username or Passwor invalid";
      }
      else{
        $username= $_POST['username'];
        $password = $_POST['password'];
        $username = stripslashes($username);
        $password = stripslashes($password);
        $username= mysql_real_escape_string($username);
        $password= mysql_real_escape_string($password);
        require_once 'connection.php';
        $query =mysql_query("SELECT * FROM test_user where username ='$username' AND password= '$password'",$conn);

        $rows= mysql_fetch_array($query);


        $count=mysql_num_rows($query);
        if($count == 1){
          $_SESSION['login_user']= $username;
          $_SESSION['userlevel']= $rows['userlevel'];
        }
        else {

          $error = "Username or Password is invalid";
        }
        mysql_close($conn);
        }
      }
     ?>

connection.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testing";

// Create connection
$conn = mysqli_connect($servername,$username,$password,$dbname) or die(mysqli_connect_error());
$sql = "SELECT id, username,password FROM test_user";
$result = $conn->query($sql);


if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "USERID : " . $row["id"]." " ."USERNAME :".$row["username"]." " ."PASSWORD :".$row["password"]."<br>";
//echo "USERNAME : " . $row["username"]." " ."email :".$row["email"]." "
//."PASSWORD :".$row["password"]." " ."date of join :".$row["date_of_join"]." " ."userlevel :".$row["userlevel"]."<br>";

    }
} else {
    echo "0 results";
}
$conn->close();
?>

显示以下错误:

  

警告:mysql_query()期望参数2是资源,第17行的C:\ xampp \ htdocs \ project_new \ PHP_new \ login.php中给出的对象

     

警告:mysql_fetch_array()期望参数1为资源,在第19行的C:\ xampp \ htdocs \ project_new \ PHP_new \ login.php中给出null

     

警告:mysql_num_rows()期望参数1为资源,在第22行的C:\ xampp \ htdocs \ project_new \ PHP_new \ login.php中给出null

     

警告:mysql_close()要求参数1为资源,第31行的C:\ xampp \ htdocs \ project_new \ PHP_new \ login.php中给出的对象

4 个答案:

答案 0 :(得分:0)

在login.php中require_once 'connection.php';之后用此

替换代码
$query = mysqli_query($conn, "SELECT * FROM test_user where username ='$username' AND password= '$password'");

    $rows = mysqli_fetch_array($query);


    $count = mysqli_num_rows($query);
    if ($count == 1) {
       // $_SESSION['login_user'] = $username;
       // $_SESSION['userlevel'] = $rows['userlevel'];
       print_r($rows);
    } else {

        $error = "Username or Password is invalid";
    }
    mysqli_close($conn);

答案 1 :(得分:0)

尝试将mysql更改为mysqli你不能完全使用mysql和mysqli。 像这样使用

$ rows = mysqli_fetch_array($ query);

希望它有帮助.. 谢谢。

答案 2 :(得分:0)

您的登录信息。尽量使用@qil

建议的mysqli
<?php
session_start();
$error="";
if(isset($_POST['submit']))
{
    if(empty($_POST['username']) || empty($_POST['password'])){
    $error = "Username or Passwor invalid";
  }
  else{

    require_once 'connection.php';

    $username= $_POST['username'];
    $password = $_POST['password'];
    $username = stripslashes($username);
    $password = stripslashes($password);
    $username= mysqli_real_escape_string($conn, $username);
    $password= mysqli_real_escape_string($conn, $password);

    $query =mysqli_query($conn, "SELECT * FROM test_user where username ='$username' AND password= '$password'");

    $rows= mysqli_fetch_array($query);


    $count=mysqli_num_rows($query);
    if($count == 1){
      $_SESSION['login_user']= $username;
      $_SESSION['userlevel']= $rows['userlevel'];
    }
    else {

      $error = "Username or Password is invalid";
    }
    mysqli_close($conn);
    }
  }
 ?>

答案 3 :(得分:0)

使用 mysqli _

混合 mysql_
  

警告mysql_query,mysql_fetch_array,mysql_connect等..扩展名   在PHP 5.5.0中已弃用,并且已在PHP 7.0.0中删除。   相反,应该使用MySQLi或PDO_MySQL扩展。

使用像这样的预备声明

<强>的login.php

<?php
    session_start();
    $error="";
    if(isset($_POST['submit']))
    {
      if(empty($_POST['username']) || empty($_POST['password'])){
        $error = "Username or Passwor invalid";
      }
      else{
            $username= $_POST['username'];
            $password = $_POST['password'];
            $username = stripslashes($username);
            $password = stripslashes($password);
            $username= mysqli_real_escape_string($conn,$username);
            $password= mysqli_real_escape_string($conn,$password);
            require_once 'connection.php';

            $stmt = $conn->prepare("SELECT * FROM test_user where username =? AND password=?");
                    $stmt->bind_param('ss',$username,$password);
                        // i - integer
                        // d - double
                        //  s - string
                       //  b - BLOB
                      //change it by respectively 
                    $stmt->execute();
            $result=$stmt->get_result();
            $row_count= $result->num_rows;

            if($row_count>0){

                $_SESSION['login_user']= $username;
                $_SESSION['userlevel']= $rows['userlevel'];

            }else {

              $error = "Username or Password is invalid";
            }

            $stmt->close();
            $conn->close();
        }
      }
     ?>