error_log表明变量没有定义

时间:2018-02-13 16:11:32

标签: php

所以我一直在处理我的php代码,目前我遇到的问题是error_log一直暗示我的变量没有定义。

我看了PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"

不幸的是,它似乎没有任何区别。

error_log中

[13-Feb-2018 11:01:44 America/New_York] PHP Notice:  Undefined variable: email in /home/beaspsaq/imgs/php/login.php on line 36
[13-Feb-2018 11:01:44 America/New_York] PHP Notice:  Undefined variable: con in /home/beaspsaq/imgs/php/login.php on line 37
[13-Feb-2018 11:01:44 America/New_York] PHP Warning:  mysqli_query() expects parameter 1 to be mysqli, null given in /home/beaspsaq/imgs/php/login.php on line 37
[13-Feb-2018 11:01:44 America/New_York] PHP Warning:  mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /home/beaspsaq/imgs/php/login.php on line 38
[13-Feb-2018 11:01:44 America/New_York] PHP Notice:  Undefined variable: con in /home/beaspsaq/imgs/php/login.php on line 81
[13-Feb-2018 11:01:44 America/New_York] PHP Warning:  mysqli_close() expects parameter 1 to be mysqli, null given in /home/beaspsaq/imgs/php/login.php on line 81
[13-Feb-2018 11:01:45 America/New_York] PHP Notice:  Undefined index: remember_me in /home/beaspsaq/imgs/php/login.php on line 16
[13-Feb-2018 11:01:45 America/New_York] PHP Warning:  mysqli_connect() expects parameter 5 to be long, string given in /home/beaspsaq/imgs/php/login.php on line 28

PHP

<?php
  session_start();

    if(isset($_SESSION["id"]) || isset($_COOKIE["id"])) {
        header("Location: dashboard.php");
    }

    if(isset($_POST["login"]))  {
        //Gather input variables
        $email = isset($_POST['email']) ? $_POST['email'] : '';
        $password = isset($_POST["password"]) ? $_POST['password'] : '';
        //Hash password as pasword should be hashed in database for security reasons.  See md5 hashing
        $hash_password = md5($password);
        $remember_me = $_POST["remember_me"];

        //Connect to Database
        $host="-";      
        $username="-"; 
        $password="-"; 
        $db_name="-"; 
        $tbl_name="-"; 

        $con = mysqli_connect("-","-","-","-","-")or die("cannot connect: " . mysqli_connect_error());
        mysqli_select_db("$db_name")or die("cannot select DB");
    }

    //Check database to see if email registered to DB
    $sql = "SELECT id FROM users WHERE email='$email' LIMIT 1";
    $query = mysqli_query($con, $sql);
    $count_users = mysqli_num_rows($query);

    if( $count_users == 1 ) {

        //Retrieve user details to perform login
        $sql = "SELECT * FROM users WHERE email='$email' LIMIT 1";
        $query = mysqli_query($con, $sql);
        while($row = mysqli_fetch_array($query)) {
            $user_id = $row["id"];
            $user_password = $row["password"];
        }


        if($hash_password == $user_password) {
            if($remember_me == 1) {
                //Set Cookie
                $cookie_name = "id";
                setcookie($cookie_name, $user_id, time() + (86400 * 30), "/");

                header("Location: dashboard.php");
            } else {
                //Set Session
                $_SESSION["id"] = $user_id;
                header("Location: dashboard.php");
            }
        } else {
            $error = '<p class="error">Password incorrect.</p>';
        }
    } else {
        $error = '<p class="error">Email address not registered.</p>';
    }

    mysqli_close($con); 
?>

2 个答案:

答案 0 :(得分:0)

一旦你$_POST["login"]启动了$ email和数据库连接。但你并不总是有POST。并且您开始使用数据库连接,无论您是否启动它。

答案 1 :(得分:0)

$email

之前声明并初始化您的if(isset($_POST["login"])){ .. }和您的数据库连接