未捕获的错误:调用成员函数buildUserData()

时间:2018-06-13 21:59:42

标签: php mysqli

好的,这就是问题所在,我正在尝试制作一个花哨的登录系统,将用户数据存储在php类中,以便于访问。但是,当我尝试调用将构建用户数据的函数时,它会抛出此异常。

致命错误:未捕获错误:在C:\ xampp \ htdocs \ cfgs \ submit \ login.php中调用字符串上的成员函数buildUserData():38堆栈跟踪:#0 {main}抛出C:\ xampp第38行\ htdocs \ cfgs \ submit \ login.php

这是userData类代码

appsettings.json

这是login.php代码

<?php

    class userData {

        public $accId = null;
        public $username = null;
        public $rank = null;
        public $vip_rank = null;
        public $email = null;
        public $auth = null;

        function buildUserData($id) {
            $result = $db->query("SELECT * FROM users WHERE id = '$id'");
            while ($row = $result->fetch_assoc()) {
                $this->accId = $id;
                $this->username = $row['username'];
                $this->rank = $row['rank'];
                $this->vip_rank = $row['rank_vip'];
                $this->email = $row['mail'];
                $this->auth = $row['auth'];
            }

        }

    }

?>

这是global.php代码

<?php

    require_once '../../global.php';

    if(!isset($_POST['submit'])) {
        header("Location: ../../index");
        return;
    } else {

        $user = $db->escapestring($_POST['user']);
        $pass = $db->escapestring($_POST['pass']);

        if (empty($user) || empty($pass)) {
            $_SESSION['logError'] = "Both fields must be filled!";
            header("Location: ../../index");
            return;
        } else {
            $result = $db->query("SELECT * FROM users WHERE username = '$user'");
            $result = $db->getrows($result);
            if ($result < 1) {
                $_SESSION['logError'] = "Username does not exist!";
                header("Location: ../../index");
                return;
            } else {
                $pass = md5($pass);
                $result = $db->query("SELECT * FROM users WHERE username = '$user' AND password = '$pass'");
                $result = $db->getrows($result);
                if ($result < 1) {
                    $_SESSION['logError'] = "Details do not match!";
                    header("Location: ../../index");
                    return;
                } else {
                    $result = $db->query("SELECT * FROM users WHERE username = '$user' AND password = '$pass'");
                    while($row = $result->fetch_assoc()){
                        $username = $row['username'];
                        $id = $row['id'];
                    } 
                    $user->buildUserData($id);
                    $_SESSION['logError'] = "Hello ". $user->username;
                    header("Location: ../../index");
                    return;
                }
            }
        }
    }

?>

最后我的数据库处理程序

    <?php

        session_start();

        require_once 'cfgs/class.database.php';
        require_once 'cfgs/class.user.php';

        $user = new userData; // I don't want to build data just yet

        $db = new database;
        $db->conn = $db->connect();


    ?>

我自己也在寻找解决方案,但事实证明这是一个难以找到你正在寻找的答案的具体事情之一。

1 个答案:

答案 0 :(得分:0)

当你初始化$ user时很好:

$user = new userData;

您稍后会覆盖变量:

$user = $db->escapestring($_POST['user']);

其中一个需要一个新名称,