ettedo2000's Profile User Rating: -----

Reputation: 0 Apprentice
Group:
Members
Active Posts:
30 (0.05 per day)
Joined:
26-September 12
Profile Views:
256
Last Active:
User is offline Yesterday, 08:55 AM
Currently:
Offline

Previous Fields

Dream Kudos:
0
Icon   ettedo2000 has not set their status

Posts I've Made

  1. In Topic: Building a "Forget Password/Username"(OOP PHP) page

    Posted 21 Jul 2014

    View Postastonecipher, on 20 July 2014 - 03:29 PM, said:

    Where did you get the DB.php code?

    Try changing line 41 to this,

    41          if($this->_query->execute( $params )) {
    


    There is some misunderstanding as well,
    11	            $email_exist = DB::getInstance()->query("SELECT email FROM users WHERE email =:email", array(":email" => $email));
    12	 
    13	            if($email_exist->count()){
    


    There sql query would be,

    "SELECT count(*) FROM users WHERE email =:email"
    


    This would return the number of rows where the email in the table matches the email passed. Where on line 13, it is misused. You could then turn the previous statement into this,

    $foundEmail = $email_exist == 1 ? True : False;
    


    Hello,
    I been following on YouTube a series of videos on OOP Login/Registration from phpacademy. I also signed up to be a member and to be allowed to implement his code. I did change a few things where I knew what I was doing or felt comfortable with. However, some of the DB.php code with the $this implementation and the PDO is very new to me.

    In order for me to learn and understand it, I decided to build onto the code with my own scripts and see how it all works and functions.

    Thank you for all your help. Till my next question
  2. In Topic: Building a "Forget Password/Username"(OOP PHP) page

    Posted 21 Jul 2014

    View PostJackOfAllTrades, on 20 July 2014 - 03:25 PM, said:



    I am starting to understand. What you are saying is I need to implement a "?" placeholder, to be passed into the query of the DB. The "?" then binds itself to the email address entered by the user by implementing the array ('email'). OOP and PDO is very new to me. I know mostly the basic PHP and MySQL connection but that is getting outdated.I am learning here by trail and errors that why I think writing your own script is the best way of learning.


    My next step is creating a random new password and replacing it into the DB, then send an email to the user with the new random password and a link for them to reset it.

    Do you have any pointers or suggestion in doing such a script? OOP and PDO is very new to me. I love the challenge to write my own script and believe its the best way to learn code,
    I really appreciate your help. If I get stuck I will post my next question on the Forum.

    Thank you
  3. In Topic: Building a "Forget Password/Username"(OOP PHP) page

    Posted 20 Jul 2014

    Thank you for the input. I was told to try this implementation to verify if email address entred is corresponding with the email address in the DB.

    	if(Input::exists()) {
    			$validate = new Validate();
    			$validation = $validate->check($_POST, array(
    				'email' => array(
    					'required' => true),
    					
    			));
    	
    			if($validation->passed()) {
    				$email = $_POST["email"];
    				$email_exist = DB::getInstance()->query("SELECT email FROM users WHERE email =:email", array(":email" => $email));
    
    				if($email_exist->count()){
    					echo "Email exists in database.";
    				} else {
    				echo "Email does not exists.";
    				}
    				echo $email;
    		}else {
    			echo 'need a email';
    		}
    	}
    
    


    However, this is now giving me a Warning message:

    Warning: PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in /home/gotoamericancenter/www/classes/DB.php on line 41

    and it tells me that email is not found in DB.Any idea what I can do the fix this?? Thank you

    Here is my DB.php
    <?php
    class DB {
    	public static $instance = null;
    
    	private 	$_pdo = null,
    				$_query = null,
    				$_error = false,
    				$_results = null,
    				$_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(PDOExeption $e) {
    			die($e->getMessage());
    		}
    	}
    
    	public static function getInstance() {
    		// Already an instance of this? Return, if not, create.
    		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++;
    					
    				}
    			}
    40
    41			if($this->_query->execute()) {
    42				$this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
    				$this->_count = $this->_query->rowCount();
    			} else {
    				$this->_error = true;
    			}
    		}
    		
    		return $this;
    	}
    	
    	public function get($table, $where) {
    		return $this->action('SELECT *', $table, $where);
    	}
    
    	public function delete($table, $where) {
    		return $this->action('DELETE', $table, $where);
    	}
    
    	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 insert($table, $fields = array()) {
    		$keys 	= array_keys($fields);
    		$values = null;
    		$x 		= 1;
    
    		foreach($fields as $value) {
    			$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 = array()) {
    		$set 	= null;
    		$x		= 1;
    
    		foreach($fields as $name => $value) {
    			$set .= "{$name} = ?";
    			if($x < count($fields)) {
    				$set .= ', ';
    			}
    			$x++;
    		}
    
    		$sql = "UPDATE users SET {$set} WHERE id = {$id}";
    
    		if(!$this->query($sql, $fields)->error()) {
    			return true;
    		}
    
    		return false;
    	}	
    	
    	public function count() {
    		// Return count
    		return $this->_count;
    	}
    	
    	public function results() {
    		// Return result object
    		return $this->_results;
    	}
    	
    	public function first() {
    		return $this->_results[0];
    	}
    	
    	public function error() {
    		return $this->_error;
    	}
    }
    ?>
    
    
  4. In Topic: Fatal error:Uncaught exception 'PDOException' and other Warrn

    Posted 10 Jul 2014

    ArtificialSoldier, after a long day, I was advised to take my php code and place it in a different section without inclosing it in a HTML <div> tag. After I did that it seems that it solved two problems with one move. Right now(you never know if this is just temporary) I don't get a serialize or unserialize PDO instances or a cannot modify header information error message.
    Thank you for explaining Session in a greater detail to me and being patient with me.
    I really hope this has solved my PDO problem if not I'll be back posting.

    P.S. I did get the date_default_timezone working.
  5. In Topic: Fatal error:Uncaught exception 'PDOException' and other Warrn

    Posted 10 Jul 2014

    Hello, I really appreciate all your input. I found "Session::put" in two of my classes. The Token and User class.

    Token.php line 5
    <?php
        class Token {
    
    	public static function generate() {
    		return Session::put(Config::get('session/token_name'), md5(uniqid()));
    	}
    
    	public static function check($token) {
    		$tokenName = Config::get('session/token_name');
    		
    		if(Session::exists($tokenName) && $token === Session::get($tokenName)) {
    			Session::delete($tokenName);
    			return true;
    		}
    		
    		return false;
    	}
    }
    ?>
    


    User.php line 75 and 81
    <?php
    /*
     * *Setup for connecting to Database and inserting fields, updating, quering and delating tables
     */
    class User {
    	private $_db,
    			$_sessionName = null,
    			$_cookieName = null,
    			$_data = array(),
    			$_isLoggedIn = false;
    
    	public function __construct($user = null) {
    		$this->_db = DB::getInstance();
    	
    		$this->_sessionName = Config::get('session/session_name');
    		$this->_cookieName = Config::get('remember/cookie_name');
    
    		// Check if a session exists and set user if so.
    		if(Session::exists($this->_sessionName) && !$user) {
    			$user = Session::get($this->_sessionName);
    
    			if($this->find($user)) {
    				$this->_isLoggedIn = true;
    			} else {
    				$this->logout();
    			}
    		} else {
    			$this->find($user);
    		}
    	}
    
    	public function exists() {
    		return (!empty($this->_data)) ? true : false;
    	}
    
    	public function find($user = null) {
    		// Check if user_id specified and grab details
    		if($user) {
    			$field = (is_numeric($user)) ? 'id' : 'username';
    			$data = $this->_db->get('users', array($field, '=', $user));
    
    			if($data->count()) {
    				$this->_data = $data->first();
    				return true;
    			}
    		}
    		return false;
    	}
    	//setup for registration Form database
    	public function create($fields = array()) {
    		if(!$this->_db->insert('users', $fields)) {
    			throw new Exception('There was a problem creating an account.');
    		}
    	}
    	//setup of contact Form database
    	public function contact($fields = array()) {
    		if(!$this->_db->insert('contact', $fields)) {
    			throw new Exception('There was a problem sending the Contact Form.');
    		}
    	}
    	//database setup for updating information
    	public function update($fields = array(), $id = null) {
    		if(!$id && $this->isLoggedIn()) {
    			$id = $this->data()->id;
    		}
    		
    		if(!$this->_db->update('users', $id, $fields)) {
    			throw new Exception('There was a problem updating.');
    		}
    	}
    
    	public function login($username = null, $password = null, $remember = false) {
    
    		if(!$username && !$password && $this->exists()) {
    			Session::put($this->_sessionName, $this->data()->id);
    		} else {
    			$user = $this->find($username);
    
    			if($user) {
    				if($this->data()->password === Hash::make($password, $this->data()->salt)) {
    					Session::put($this->_sessionName, $this->data()->id);
    
    					if($remember) {
    						$hash = Hash::unique();
    						$hashCheck = $this->_db->get('users_session', array('user_id', '=', $this->data()->id));
    
    						if(!$hashCheck->count()) {
    							$this->_db->insert('users_session', array(
    								'user_id' => $this->data()->id,
    								'hash' => $hash
    							));
    						} else {
    							$hash = $hashCheck->first()->hash;
    						}
    
    						Cookie::put($this->_cookieName, $hash, Config::get('remember/cookie_expiry'));
    					}
    
    					return true;
    				}
    			}
    		}
    
    		return false;
    	}
    
    	public function hasPermission($key) {
    		$group = $this->_db->query("SELECT * FROM groups WHERE id = ?", array($this->data()->group));
    		
    		if($group->count()) {
    			$permissions = json_decode($group->first()->permissions, true);
    
    			if($permissions[$key] === 1) {
    				return true;
    			}
    		}
    
    		return false;
    	}
    
    	public function isLoggedIn() {
    		return $this->_isLoggedIn;
    	}
    
    	public function data() {
    		return $this->_data;
    	}
    
    	public function logout() {
    		$this->_db->delete('users_session', array('user_id', '=', $this->data()->id));
    
    		Cookie::delete($this->_cookieName);
    		Session::delete($this->_sessionName);
    	}
    }
    ?>
    


    How would I change this code to stop the error from occurring? I always having trouble with understanding the full function of Sessions. I am still learning and everyday I get a little smarter, or so I hope. :balloon:/>

My Information

Member Title:
New D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Private

Friends

ettedo2000 hasn't added any friends yet.

Comments

ettedo2000 has no profile comments yet. Why not say hello?