It's not different as the same error listed here, but I followed every advise I got from it and still didn't find the mistake I made, so therefore it's different
I'm following a series of videos on phpacademy.org, ([https://www.youtube.com/watch?v=G3hkHIoDi6M&index=15&list=PLfdtiltiRHWF5Rhuk7k4UAU1_yLAZzhWc][1])
In the video above (number 15), after following it, I get this error
Fatal error: Call to a member function insert() on a non-object in D:\xampp\htdocs\scorpio to do\classes\user.php on line 10
my code is: 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>
everything worked fine until about min 13 of video, where he does an echo of the hash of salt (I see the hash, so that works), so I think the mistake is somewhere in the create function in user.php or in the try/catch of register.php, it's probably a stupid mistake, comma or something I forgot, but I looked over the video and my code a few times, but can't find it, hope you guys can help me out
database is loaded automaticly in db file and that works, cause I also have done validation file already and those db functions work just fine
edit: I don't think something went wrong in the db code, but just to make shure, here's the code:
<?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';
?>