A lot of errors on login form

时间:2017-05-16 09:25:10

标签: php mysqli

For a school assignment I have to make a website for a photographer. I also have to make a small admin panel where he can upload his photo's. But i get a lot of errors and I don't know how to fix them. Been trying for about 5 days.. Here is my code in de admin_login.php

<?php

session_start();

if (isset($_SESSION["manager"])) {
    header("location: index.php");
    exit();
}

if (isset($_POST["username"]) && isset($_POST["password"])) {

    $manager = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["username"]); // filter everything but numbers and letters
    $password = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["password"]); // filter everything but numbers and letters
    // Connect to the MySQL database  
    include "../storescripts/connect_to_mysql.php";
    $sql = 'mysqli_query($connect, $sql_query)';
    $query = "SELECT id FROM admin WHERE 1 username='$manager' AND password='$password'";


    // ------- MAKE SURE PERSON EXISTS IN DATABASE ---------
    $existCount = mysqli_num_rows($sql); // count the row nums
    if ($existCount == 1) { // evaluate the count
        while ($row = mysqli_fetch_array($sql)) {
            $id = $row["id"];
        }
        $_SESSION["id"] = $id;
        $_SESSION["manager"] = $manager;
        $_SESSION["password"] = $password;
        header("location: index.php");
        exit();
    } else {
        echo 'That information is incorrect, try again <a href="index.php">Click Here</a>';
        exit();
    }
}

Here is the connect_to_mysql.php

<?php
    $con = mysqli_connect("localhost","**","**","**");

    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
    ?>

It will give this errors when I try to login:

Warning: mysqli_connect(): MySQL server has gone away in /storage/h10/617/1235617/public_html/storescripts/connect_to_mysql.php on line 2

Warning: mysqli_connect(): Error while reading greeting packet. PID=46585 in /storage/h10/617/1235617/public_html/storescripts/connect_to_mysql.php on line 2

Warning: mysqli_connect(): (HY000/2006): MySQL server has gone away in /storage/h10/617/1235617/public_html/storescripts/connect_to_mysql.php on line 2 Failed to connect to MySQL: MySQL server has gone away

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, string given in /storage/h10/617/1235617/public_html/storeadmin/admin_login.php on line 22

I have no idea where it goes wrong, any help would be appreciated

1 个答案:

答案 0 :(得分:0)

试试这个,$sql = mysqli_query($connect, $query);中的错误代码就像$ sql = 'mysqli_query($connect, $query)';一样,我认为你不必在index.php中检查SESSION,因为你要登录。并且在成功header('Location:to_other_page.php')之后再次登录不再是index.php。

 <?php
     // Connect to the MySQL database  
    include "../storescripts/connect_to_mysql.php";
    if (isset($_POST["username"]) && isset($_POST["password"])) {

        $manager = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["username"]); // filter everything but numbers and letters
        $password = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["password"]); // filter everything but numbers and letters

        // make the query
        $query = "SELECT * FROM admin WHERE username='$manager' AND password='$password'";
        // run the query
        $sql = mysqli_query($connect, $query);

        // ------- MAKE SURE PERSON EXISTS IN DATABASE ---------
        $existCount = mysqli_num_rows($sql); // count the row nums
        if ($existCount) {
             session_start();
             session_regenerate_id() = true;
             $_SESSION["id"] = session_id();
             $_SESSION["manager"] = $manager;
             $_SESSION["password"] = $password;
             header("location: your_file.php");  // not index.php, you are not going back to index page after login
             exit();
        } else {
            echo 'That information is incorrect, try again <a href="index.php">Click Here</a>';
            exit();
        }
    }
    ?>

connect_to_mysql.php中,您与$con有联系,admin_login.php$connect。这就是错误。
将其放在connect_to_mysql.php中,并用您的db用户,db密码和db名称替换**

<?php
    $connect = mysqli_connect("localhost","**","**","**");
    // Check connection
    if (!$connect)
    {
        die ("Failed connect to database");
    }
?>