致命错误:在D:\ xampp \ htdocs \ scorpio中的非对象上调用成员函数insert()以在第10行执行\ classes \ user.php

时间:2014-09-01 13:47:41

标签: php mysql database mysqli

与此处列出的相同错误并没有什么不同,但我遵循了我从中得到的所有建议,但仍然没有发现我犯的错误,因此它有所不同

我关注了phpacademy.org上的一系列视频,([https://www.youtube.com/watch?v=G3hkHIoDi6M&index=15&list=PLfdtiltiRHWF5Rhuk7k4UAU1_yLAZzhWc][1]

在上面的视频(编号15)中,在关注之后,我收到此错误

  

致命错误:在D:\ xampp \ htdocs \ scorpio中的非对象上调用成员函数insert()以在第10行执行\ classes \ user.php

我的代码是: user.php的:

<?php
class user{
    private $_db;

    public function __construct(){
        $this->_db = db::getInstance();
    }


    //PROBLEM MIGHT BE HERE!!!!!!!!!!!!!!!

    public function create($fields = array()){
        if(!$this->_db->insert('gebruikers', $fields)){
            throw new Exception('Er is een probleem opgetreden bij het maken van de     nieuwe gebruiker.');
            }
        }
    }
?>

register.php:

<?php
require_once 'core/init.php';

if(input::exists()){
    if(token::check(input::get('token'))){

        $validate = new validate();
        $validation = $validate->check($_POST, array(
            'gebruikersnaam' => array('required' => true, 'min' => 2, 'max' => 20,     'unique' => 'gebruikers'),
            'paswoord' => array('required' => true, 'min' => 6),
            'paswoord_2' => array('required' => true, 'matches' => 'paswoord'),
            'naam' => array('required' => true, 'min' => 2, 'max' => 50),
        ));

        if($validation->passed()){
            $user = new user();
            $salt = hash::salt(32);



            //I THINK THE PROBLEM IS IN THIS TRY CATCH OR IN USER.PHP!!!!!!!!!!!!!!!!!

            try{                    
                $user->create(array(
                    'gebruikersnaam' => input::get('gebruikersnaam'),
                    'paswoord' => hash::make(input::get('paswoord'), $salt),
                    'salt' => $salt,
                    'voornaam' => input::get('naam'),
                    'aangemaakt' => date('Y-m-d H:i:s'),
                    'groep' => 1
                ));

                session::flash('home', 'Je bent geregistreerd en kan je nu aanmelden.');
                header('Location: index.php');
            }catch(Exception $e){
                die($e->getMessage());
            }
        }else{
            foreach($validation->errors() as $error){
                echo $error, '<br />';
            }
        }
    }
}
?>

<form action="" method="post">
    <div class="field">
        <label for="gebruikersnaam">Gebruikersnaam</label>
        <input type="text" name="gebruikersnaam" id="gebruikersnaam" value="<?php echo     escape(input::get('gebruikersnaam')); ?>" autocomplete="off" />
    </div>

    <div class="field">
        <label for="paswoord">Paswoord</label>
        <input type="password" name="paswoord" id="paswoord" />
    </div>

    <div class="field">
        <label for="paswoord_2">Paswoord herhalen</label>
        <input type="password" name="paswoord_2" id="paswoord_2" />
    </div>

    <div class="field">
        <label for="naam">Voornaam</label>
        <input type="text" name="naam" id="naam" value="<?php echo     escape(input::get('naam')); ?>" />
    </div>

    <input type="hidden" name="token" value="<?php echo token::generate(); ?>">
    <input type="submit" value="Registreren" />
</form>

一切正常,直到大约13分钟的视频,在那里他做了一个盐的哈希回声(我看到哈希值,这样才有效),所以我认为错误是在user.php的create函数中的某处或在register.php的try / catch中,它可能是一个愚蠢的错误,逗号或我忘了的东西,但我查看了几次视频和我的代码,但无法找到它,希望你伙计们可以帮助我

数据库在db文件中自动加载并且有效,因为我已经完成了验证文件并且那些db函数工作正常

编辑:我不认为数据库代码出了问题,只是为了让shure成为现实代码:

<?php
class db{
    private static $_instance = null;
    private $_pdo, 
            $_query, 
            $_error = false, 
            $_results, 
            $_count = 0;

    private function __construct(){
        try{
            $this->_pdo = new PDO('mysql:host='. config::get('mysql/host') .';dbname='. config::get('mysql/db'), config::get('mysql/username'), config::get('mysql/password'));
        } catch(PDOException $e){
            die($e->getMessage());
        }
    }

    public static function getInstance(){
        if(!isset(self::$_instance)){
            return self::$_instance = new db();
        }
    }

    public function query($sql, $params = array()){
        $this->_error = false;
        if($this->_query = $this->_pdo->prepare($sql)){
            $x = 1;
            if(count($params)){
                foreach($params as $param){
                    $this->_query->bindValue($x, $param);
                    $x++;
                }
            }

            if($this->_query->execute()){
                $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
                $this->_count = $this->_query->rowCount();
            }else{
                $this->_error = true;
            }
        }

        return $this;
    }

    public function action($action, $table, $where = array()){
        if(count($where) === 3){
            $operators = array('=', '>', '<', '>=', '<=');

            $field = $where[0];
            $operator = $where[1];
            $value = $where[2];

            if(in_array($operator, $operators)){
                $sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?";

                if(!$this->query($sql, array($value))->error()){
                        return $this;
                }
            }
        }
        return false;
    }

    public function get($table, $where){
        return $this->action('SELECT *', $table, $where);
    }

    public function delete($table, $where){
        return $this->action('DELETE', $table, $where);
    }

    public function insert($table, $fields = array()){
            $keys = array_keys($fields);
            $values = '';
            $x = 1;

            foreach($fields as $field){
                $values .= '?';
                if($x < count($fields)){
                    $values .= ', ';
                }
                $x++;
            }

            $sql = "INSERT INTO {$table} (`". implode('`,`', $keys) ."`) VALUES ({$values})";

            if(!$this->query($sql, $fields)->error()){
                return true;
            }
        return false;
    }

    public function update($table, $id, $fields){
        $set = '';
        $x = 1;

        foreach($fields as $name => $value){
            $set .= "{$name} = ?";
            if($x < count($fields)){
                $set .= ', ';
            }
            $x++;
        }

        $sql = "UPDATE {$table} SET {$set} WHERE id = {$id}";

        if(!$this->query($sql, $fields)->error()){
            return true;
        }

        return false;
    }

    public function results(){
        return $this->_results;
    }

    public function first(){
        return $this->results()[0];
    }

    public function error(){
        return $this->_error;
    }

    public function count(){
        return $this->_count;
    }
}
?>

的init.php:

<?php
session_start();

ini_set('display_errors', 1);
error_reporting(E_ALL);

$GLOBALS['config'] = array(
    'mysql' => array(
    'host' => 'localhost',
    'username' => '*****',
    'password' => '*****',
    'db' => '*****'
),
'remember' => array(
    'cookie_name' => 'hash',
    'cookie_expiry' => 604800
),
'session' => array(
    'session_name' => 'user',
    'token_name' => 'token'
)
);

spl_autoload_register(function($class){
require_once 'classes/' . $class . '.php';
});

require_once 'functions/sanitize.php';
?>

1 个答案:

答案 0 :(得分:-1)