php - undefined variable ?

标签 php mysql

我在第 15 行和第 21 行收到 $id 变量的 undefined variable 错误,有人可以解释为什么吗?我看不出问题出在哪里。

<?php
function userIsLoggedIn()
{
    if (isset($_POST['action']) and $_POST['action'] == 'login')
    {
        if (!isset($_POST['email']) or $_POST['email'] == '' or
            !isset($_POST['password']) or $_POST['password'] == '')
        {
            $GLOBALS['loginError'] = 'Please fill in both fields';
            return FALSE;
        }
        $password = md5($_POST['password'] . 'chainfire db');

        if (databaseContainsAuthor($_POST['email'], $password, $id))
        {   
        include 'db.inc.php';
            session_start();
            $_SESSION['loggedIn'] = TRUE;
            $_SESSION['email'] = $_POST['email'];  
            $_SESSION['password'] = $password;
            $_SESSION['id'] = $id;
            return TRUE;
        }
        else
        {
            session_start();
            unset($_SESSION['loggedIn']);
            unset($_SESSION['email']);
            unset($_SESSION['password']);
            unset($_SESSION['id']);
            $GLOBALS['loginError'] = 'The specified email address or password was incorrect.';
            return FALSE;
        }
    }
    if (isset($_POST['action']) and $_POST['action'] == 'logout')
    {
        session_start();
        unset($_SESSION['loggedIn']);
        unset($_SESSION['email']);
        unset($_SESSION['password']);
        unset($_SESSION['id']);
        header('Location: ' . $_POST['goto']);
        exit();
    }
    session_start();
    if (isset($_SESSION['loggedIn']))
    {
        return databaseContainsAuthor($_SESSION['email'], $_SESSION['password'], $_SESSION['id']);
    }
}
function databaseContainsAuthor($email, $password, $id)
{
    include 'db.inc.php';

    $email = mysqli_real_escape_string($link, $email);
    $password = mysqli_real_escape_string($link, $password);

    $sql = "SELECT COUNT(*) FROM author
            WHERE email='$email' AND password='$password'";
    $result = mysqli_query($link, $sql);

    if (!$result)
    {
        $error = 'Error searching for author.';
        include 'error.html.php';
        exit();
    }
    $row = mysqli_fetch_array($result);

    $sql = "SELECT id FROM author 
            WHERE email='$email'"; 
    $id = mysqli_query($link, $sql);
    if (!$id)
    {
        $error = 'Error searching for id.';
        include 'error.html.php';
        exit();
    }    

    if ($row[0] > 0)
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

变量$id定义在databaseContainsAuthor($email, $password, $id)中,然后存储在$_SESSION['id'] session 如此自然 $id = mysqli_query($link, $sql); 应该已经通过了,但事实并非如此?

最佳答案

在函数内更改(或定义)的变量不会影响脚本的其余部分。例如:

<?php
function changeVariabe($person) {
    $person = 'Bob';
}
$person = 'Alice';
changeVariable($person);
echo "Hello $person!"; // Outputs: Hello Alice!

这可以通过引用传递变量来避免,如下所示:

<?php
function changeVariabe(&$person) {
    $person = 'Bob';
}
$person = 'Alice';
changeVariable($person);
echo "Hello $person!"; // Outputs: Hello Bob!

你也可以使用全局变量,像这样:

<?php
function changeVariabe() {
    global $person;
    $person = 'Bob';
}
$person = 'Alice';
changeVariable();
echo "Hello $person!"; // Outputs: Hello Bob!

关于php - undefined variable ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6968095/

相关文章:

php - 当我将 print_r() 应用于 PHP 中的数组时,为什么会得到 "Resource id #4"?

java - Netbeans 的 Hibernate Unicode;为什么在db中保存为 "?????"

php - 如何用数组查询mysql表?

php - 使用 while 循环插入数据库

php - 使用 php 对远程机器进行无密码 ssh 访问?

php - 未找到包含内部 namespace

php - Paypal 沙盒 IPN : 14077410:ssl routines:ssl23_get_server_hello:sslv3 alert handshake failure

php - Mysql 在两个日期和时间之间进行搜索

php - Laravel 和多个数据库连接

php - 在mysql中存储IP地址?