Jump to content

OOP Login and Register tutorial error

CripDawg

I'm doing this php academy login tutorial I'm upto this part 

 the echo success at 4:00 works but the next one at 6:40 doesn't work i get a blank screen and no errors my code is the same i even ran the SQL query within the database and it returns my dummy account 

 

DB.php

<?phpclass 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)) {self:_instance = new DB();}return self:_instance;} 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()) {  echo 'Success';  }}} }

index.php

<?phprequire_once 'core/init.php'; DB::getInstance()->query("SELECT username FROM users WHERE username = ?", array('joseph'));
Link to comment
Share on other sites

Link to post
Share on other sites

What is your database structure? What is in the database? What query works, what query doesn't work?

Link to comment
Share on other sites

Link to post
Share on other sites

What is your database structure? What is in the database? What query works, what query doesn't work?

my database contains an id,username, password group as the tutorial specifies this is the query that works 

 

SELECT username FROM users WHERE username = 'joseph' works it returns the only entry in the database as i have not gotten up to the register part yet 

Link to comment
Share on other sites

Link to post
Share on other sites

my database contains an id,username, password group as the tutorial specifies this is the query that works 

 

SELECT username FROM users WHERE username = 'joseph' works it returns the only entry in the database as i have not gotten up to the register part yet 

 

So what isn't working, then?

Link to comment
Share on other sites

Link to post
Share on other sites

So what isn't working, then?

i don't know i get no errors when i run it in my browser just a blank page when it should echo Success just like the guy in the tutorial 

Link to comment
Share on other sites

Link to post
Share on other sites

i don't know i get no errors when i run it in my browser just a blank page when it should echo Success just like the guy in the tutorial 

 

Most of the time a blank page means that php faild hard somewhere (500 or so). Check the error logs of your Webserver.

If you use XAMPP on Windows the logs should be here: xampp\apache\logs\error.log (where xampp\ is your install dir), on linux they should be located here: /var/log/httpd/error_log or /var/log/apache2/error_log, depending on distro and setup.

Link to comment
Share on other sites

Link to post
Share on other sites

Most of the time a blank page means that php faild hard somewhere (500 or so). Check the error logs of your Webserver.

If you use XAMPP on Windows the logs should be here: xampp\apache\logs\error.log (where xampp\ is your install dir), on linux they should be located here: /var/log/httpd/error_log or /var/log/apache2/error_log, depending on distro and setup.

i fixed it missed out a equals sign

Link to comment
Share on other sites

Link to post
Share on other sites

Most of the time a blank page means that php faild hard somewhere (500 or so). Check the error logs of your Webserver.

If you use XAMPP on Windows the logs should be here: xampp\apache\logs\error.log (where xampp\ is your install dir), on linux they should be located here: /var/log/httpd/error_log or /var/log/apache2/error_log, depending on distro and setup.

 

So what isn't working, then?

guys im getting this error now tried googling it didnt make any sense to me :(

 

Fatal error: Call to undefined method DB::fetchALL() in /Applications/XAMPP/xamppfiles/htdocs/xampp/ooplr/classes/DB.php on line 39

 

<?phpclass 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)) {self:_instance = new DB();}return self:_instance;} 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->fetchALL(PDO::FETCH_OBJ);      $this->_count = $this->_query->rowCount(); } else {  $this->_error = true; }} return $this;} public function error() {return $this->_error;} }
Link to comment
Share on other sites

Link to post
Share on other sites

Fatal error: Call to undefined method DB::fetchALL() in /Applications/XAMPP/xamppfiles/htdocs/xampp/ooplr/classes/DB.php on line 39

 

Your database class doesn't have a method called fetchALL(), does it? You probably need to call in on $this->_query

Link to comment
Share on other sites

Link to post
Share on other sites

Your database class doesn't have a method called fetchALL(), does it? You probably need to call in on $this->_query

the guy in the tutorial he call any method named 'fetchALL' i watched the video over a million times 

Link to comment
Share on other sites

Link to post
Share on other sites

the guy in the tutorial he call any method named 'fetchALL' i watched the video over a million times 

 

I meant you need to call:

$this->_query->->fetchAll();

after executing the query.

 

This code:

$this->fetchAll();

implies that your database has a method "fetchAll()"; but it doesn't.

Link to comment
Share on other sites

Link to post
Share on other sites

I meant you need to call:

$this->_query->->fetchAll();

after executing the query.

 

This code:

$this->fetchAll();

implies that your database has a method "fetchAll()"; but it doesn't.

great that worked ive gone a fair way in the tutorail and are getting some more errors that i dont know how to fix

 

Warning: Missing argument 3 for DB::update(), called in /Applications/XAMPP/xamppfiles/htdocs/xampp/ooplr/index.php on line 7 and defined in/Applications/XAMPP/xamppfiles/htdocs/xampp/ooplr/classes/DB.php on line 102

Notice: Undefined variable: fields in /Applications/XAMPP/xamppfiles/htdocs/xampp/ooplr/classes/DB.php on line 106

Warning: Invalid argument supplied for foreach() in /Applications/XAMPP/xamppfiles/htdocs/xampp/ooplr/classes/DB.php on line 106

 

<?phpclass 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)) {self:_instance = new DB();}return self:_instance;} 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 users (`" . 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++;}die($set); $sql = "UPDATE {$table} SET {$set} WHERE id = {$id}"; } public function results() {return $this->_results;} public function error() {return $this->_error;} public function count() {return $this->_count;}  }
Link to comment
Share on other sites

Link to post
Share on other sites

Warning: Missing argument 3 for DB::update(), called in /Applications/XAMPP/xamppfiles/htdocs/xampp/ooplr/index.php on line 7 and defined in/Applications/XAMPP/xamppfiles/htdocs/xampp/ooplr/classes/DB.php on line 102

How many arguments does update() take? What are they? What and How many are you passing when you call it?

 

Notice: Undefined variable: fields in /Applications/XAMPP/xamppfiles/htdocs/xampp/ooplr/classes/DB.php on line 106

Notice any relationship between this and the previous error?

Warning: Invalid argument supplied for foreach() in /Applications/XAMPP/xamppfiles/htdocs/xampp/ooplr/classes/DB.php on line 106

Same again.

Link to comment
Share on other sites

Link to post
Share on other sites

 

Warning: Missing argument 3 for DB::update(), called in /Applications/XAMPP/xamppfiles/htdocs/xampp/ooplr/index.php on line 7 and defined in/Applications/XAMPP/xamppfiles/htdocs/xampp/ooplr/classes/DB.php on line 102

How many arguments does update() take? What are they? What and How many are you passing when you call it?

 

Notice: Undefined variable: fields in /Applications/XAMPP/xamppfiles/htdocs/xampp/ooplr/classes/DB.php on line 106

Notice any relationship between this and the previous error?

Warning: Invalid argument supplied for foreach() in /Applications/XAMPP/xamppfiles/htdocs/xampp/ooplr/classes/DB.php on line 106

Same again.

 

this next bit is not really a error the form is not doing what its supposed to its meant to echo submitted when the submit button is pressed however it does it when the form is loaded

 

input.php

<?phpclass Input {public static function exists($type = 'post'){switch ($type) {case 'post';return (!empty($_POST)) ? true : false;break; case 'get';return (!empty($_GET)) ? true : false;break; default:return false;break;} }}

register.php

<?phprequire_once 'core/init.php'; if(input::exists()) {echo 'submitted';}?><form action ="" method= "post"><div class="field"><label for= "username">Username</label><input type= "text" name= "username" id ="username" value="" autocomplete="off"></div> <div class="field"><label for="password">Choose a password</label><input type="password" name="password" id="password"> <div class="field"><label for="password_again">Confirm password</label><input type="password" name="password_again" id="password_again"> <div class="field"><label for= "name">Name</label><input type= "text" name= "name" value="" id ="name"></div><input type="submit" value="Register"></form> 
Link to comment
Share on other sites

Link to post
Share on other sites

le snip

 

Not sure, I can't reproduce the issue; but those semi-colons after each case in your select should be colons.

Link to comment
Share on other sites

Link to post
Share on other sites

Not sure, I can't reproduce the issue; but those semi-colons after each case in your select should be colons.

now ive got a new problem this should echo the requirements for each field eg. username required must be 1 however it does not echo the rule value 

 

 

FIXED

output:

username required must be {rule_value}

 

register.php

<?phprequire_once 'core/init.php'; if(Input::exists()) {$validate = new Validate();$validation = $validate->check($_POST, array('username' => array('required' => true,'min' => 15,'max' => 20,'unique' => 'users'),'password' => array('required' => true,'min' => 6),'password_again' => array('required' => true,'matches' => 'password'),'name' => array('required' => true,'min' => 2,'max' => 50))); if($validation->passed()) {//register user} else{// output errors}}?><form action ="" method= "post"><div class="field"><label for= "username">Username</label><input type= "text" name= "username" id ="username" value="" autocomplete="off"></div> <div class="field"><label for="password">Choose a password</label><input type="password" name="password" id="password"> <div class="field"><label for="password_again">Confirm password</label><input type="password" name="password_again" id="password_again"> <div class="field"><label for= "name">Name</label><input type= "text" name= "name" value="" id ="name"></div><input type="submit" value="Register"></form>  

Validate.php

<?phpclass Validate{private $_passed,$_errors = array(),$_db = null;public function __construct() {$this->_db = DB::getInstance();} public function check($source, $items = array()) {foreach($items as $item => $rules) {foreach($rules as $rule => $rule_value) {echo "{$item} {$rule} must be {rule_value}<br>";}}} }
Link to comment
Share on other sites

Link to post
Share on other sites

Most of the time a blank page means that php faild hard somewhere (500 or so). Check the error logs of your Webserver.

If you use XAMPP on Windows the logs should be here: xampp\apache\logs\error.log (where xampp\ is your install dir), on linux they should be located here: /var/log/httpd/error_log or /var/log/apache2/error_log, depending on distro and setup.

php has its own error log for xampp

xampp\php\logs\php_error_log

i want to die

Link to comment
Share on other sites

Link to post
Share on other sites

Not sure, I can't reproduce the issue; but those semi-colons after each case in your select should be colons.

getting a really wired error from a file that i haven't changed at all during this file creation and was working fine this only started happening when i added the match for the passwords then i deleted it to see if the error was resolved and the error was still there also it appears in one of my text boxes with BR tags in between the words

 

Notice:  Undefined index: username in /Applications/XAMPP/xamppfiles/htdocs/xampp/ooplr/classes/Input.php on line 23

Notice:  Undefined index: name in /Applications/XAMPP/xamppfiles/htdocs/xampp/ooplr/classes/Input.php on line 23

 

Validate.php

<?phpclass Validate{private $_passed = false,$_errors = array(),$_db = null;public function __construct() {$this->_db = DB::getInstance();} public function check($source, $items = array()) {foreach($items as $item => $rules) {foreach($rules as $rule => $rule_value) { $value = trim($source[$item]); if($rule === 'required' && empty($value)) {$this->addError("{$item} is required");} else if(!empty($value)) {switch($rule) {case 'max':if (strlen($value) > $rule_value) {$this->addError("{$item} must be a maximum of {$rule_value} charcters");}break;case 'min':if (strlen($value) < $rule_value) {$this->addError("{$item} must be a minimum of {$rule_value} characters");}break;case 'matches':if($value != $source[$rule_value]) {$this->addError("{$rule_value} must match {$item}");}break;case 'unique': break;}}}} if (empty($this->_errors)) {$this->_passed = true;} return $this;} private function addError($error) {$this->_errors[] = $error; } public function errors() {return $this->_errors;} public function passed(){return $this->_passed;}}  

 

register.php

<?phprequire_once 'core/init.php'; if(Input::exists()) {$validate = new Validate();$validation = $validate->check($_POST, array('username' => array('required' => true,'min' => 6,'max' => 20,'unique' => 'users'),'password' => array('required' => true,'min' => 6),'password_again' => array('required' => true,'matches' => 'password'),'name' => array('required' => true,'min' => 2,'max' => 50))); if($validation->passed()) {echo "passed";} else {print_r($validation->errors());}}?><form action ="" method= "post"><div class="field"><label for= "username">Username</label><input type= "text" name= "username" id ="username" value="<?php echo escape(Input::get('username')); ?>" autocomplete="off"></div> <div class="field"><label for="password">Choose a password</label><input type="password" name="password" id="password"> <div class="field"><label for="password_again">Confirm password</label><input type="password" name="password_again" id="password_again"> <div class="field"><label for= "name">Name</label><input type= "text" name= "name" value="<?php echo escape(Input::get('name')); ?>" id ="name"></div><input type="submit" value="Register"></form>

 

Input.php

 

<?phpclass Input {public static function exists($type = 'post'){switch ($type) {case 'post':return (!empty($_POST)) ? true : false;break;case 'get':return (!empty($_GET)) ? true : false;break; default:return false;break;} } public static function get($item) {if(isset($_POST[$item])) {return $_POST[$item];} elseif(isset($_GET)) {return $_GET[$item];}return ''; }}
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×