www.webdeveloper.com
Results 1 to 2 of 2

Thread: Account Creation

Hybrid View

  1. #1
    Join Date
    Jul 2013
    Posts
    1

    Account Creation

    I am having a bit of trouble getting this register function to work. Its for a game server so the password has to be md5 and salted with the username. Here is the function part of the Register.
    This is using MVC by the way.
    Code:
    <?php
    
    class Register extends MainController{
    	function __construct(){
    						debug_backtrace();
    			parent::__construct();
    			$this->view->url	=	$this->config->url;
    			$this->view->ID 	  = get_class($this);
    			$this->view->Title = "Royal JD > Create Account";
    			$this->view->render('Register');
    
    	function Create_Account(){
    		$username = isset($_POST['username']) ? $_POST['username'] : '';
    		$email = isset($_POST['email']) ? $_POST['email'] : '';
    		$password = isset($_POST['password']) ? $_POST['password'] : '';
    		$username = mysql_real_escape_string(StrToLower(Trim($username)));
    		$password = mysql_real_escape_string(StrToLower(Trim($password)));
    
    		$this->view->salt = $username;
    		$this->view->salt = $username.$password;
    		$this->view->salt = md5($this->view->salt);
    		$this->view->salt = "0x".$this->view->salt; //Salts the password in md5.
    		
    			$sql = "INSERT INTO users WHERE name = :name AND passwd = :passwd AND idnumber = :idnumber AND email = :email";
    			$arr = array(":name" => $username, ":email" => $email, ":idnumber" => $password, ":passwd" => $this->view->salt);
    			$ctr = $this->database->DBQry($sql,$arr);
    		}
    	
    }
    }
    ?>
    the database.php looks like this:

    Code:
    <?php
    
    
    	class Database extends PDO{
    		
    		// Initialize
    		const DBHOST  = "localhost";
    		const DBNAME  = "test";
    		const DBUSER  = "root";
    		const DBPASS  = "123";
    		
    		const SELECT  = "SELECT * FROM ";
    		const INSERT  = "INSERT INTO ";
    		const UPDATE  = "UPDATE ";
    		const DELETE  = "DELETE FROM ";
    		
    		
    		// Make Connection
    		function __construct(){
    			parent::__construct("mysql:host=".self::DBHOST.";dbname=".self::DBNAME,self::DBUSER,self::DBPASS);
    		}
    		
    		// Query
    		function DBQry($sql,$arr){
    			$sth = $this->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
    			$sth->execute($arr);
    			$rs = $sth->fetchAll();
    			return $rs;
    		}
    		
    		// Count
    		function DBCtr($sql,$arr){
    			$sth = self::prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
    			$sth->execute($arr);
    			return $sth->rowCount();
    		}
    		
    		// Last ID
    		function DBLst($table){
    			$sql = $this->SELECT.$table." ORDER BY id DESC LIMIT 1";
    			$arr = array();
    			$ctr = self::DBCtr($sql,$arr);
    			$rs =  self::DBQry($sql,$arr);
    			return ($ctr > 0) ? $rs[0]['id'] : 0;
    		}
    		
    		// Insert
    		function DBIns($arr,$tbl){
    			$fld = 	implode(",",array_keys($arr));
    			$sql =  self::INSERT.$tbl." (".str_replace(":","",$fld).")
    					 VALUES(".$fld.")";
    					 
    			$sth = $this->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
    			$sth->execute($arr);
    		}
    		
    		// Update
    		function DBSet($arr,$tbl,$whr = ''){
    			$stmt = array();
    			foreach($arr as $fld => $val){
    				$stmt[] =  str_replace(":","",$fld)." = ".$fld;
    			}
    			$stm =  implode(",",$stmt);
    			$sql =  self::UPDATE.$tbl." SET ".$stm.' '.$whr;
    			$sth =  $this->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
    			$sth->execute($arr);
    		}
    		
    		// Delete
    		function DBDel($tbl,$whr = ''){
    			$sql = self::DELETE.$tbl." ".$whr;
    			$this->exec($sql);
    		}
    		
    		
    	
    		
    	}
    
    
    
    ?>
    My issue is that its not sending any info to the database.

    Any help will be appreciated!

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    Nothing immediately jumps out at me as wrong, so I'd recommend adding some checking of return values and error_log()-ing (or just die()-ing for now?)
    PHP Code:
            function DBQry($sql,$arr){
                
    $sth $this->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
                if(
    $sth == false) {
                    
    $msg $this->errorInfo();
                    
    error_log("Prepare failed".PHP_EOL.print_r($msgtrue).PHP_EOL.$sql); // die() instead if you just want simple debugging for now
                    
    return false;
                }
                if(
    $sth->execute($arr) == false) {
                    
    $msg =$sth->errorInfo();
                    
    error_log("Execute failed".PHP_EOL.print_r($msgtrue).PHP_EOL.$sql);
                    return 
    false;
                }
                
    $rs $sth->fetchAll();
                return 
    $rs;
            } 
    (You could move some of the error logging/output stuff into a separate method and just call it with the desired parameters in order to clean it up and avoiding repetitious code.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles