Account Creation

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 572 Views - Last Post: 22 July 2013 - 09:20 AM Rate Topic: -----

#1 Hulu  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 17-July 13

Account Creation

Posted 17 July 2013 - 06:42 PM

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.
<?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:

<?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!

Is This A Good Question/Topic? 0
  • +

Replies To: Account Creation

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3637
  • View blog
  • Posts: 5,766
  • Joined: 08-June 10

Re: Account Creation

Posted 17 July 2013 - 06:48 PM

One thing in there seems fairly odd. The Create_Account function is defined inside the constructor. Just see what happens if you apply proper indentation to the code you posted:
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);
        }
    }
}


Surely this isn't supposed to be like that?
Was This Post Helpful? 0
  • +
  • -

#3 Hulu  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 17-July 13

Re: Account Creation

Posted 17 July 2013 - 06:55 PM

<?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);
		}
	
}
}
?>


It does the same thing.

This post has been edited by Dormilich: 17 July 2013 - 10:03 PM

Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3637
  • View blog
  • Posts: 5,766
  • Joined: 08-June 10

Re: Account Creation

Posted 17 July 2013 - 07:40 PM

Of course it does. I didn't change the code, I just altered the indentation to make the problem I was pointing out more obvious. - It's very important to make sure the code is formatter sanely. It makes a lot of otherwise hard to spot issues obvious. In your case, the fact that you put the Create_Account function at the same indentation level as the constructor makes it look like both are member functions of the Register class, while in fact the Create_Account function is being defined as a global function when instances of the Register class are created.

If you were to create two instances of the Register class, you would get a fatal error because the constructor is defining the global Create_Account function each time.

This also brings up the topic of visibility keywords. Had you specified, say, public on your Create_Account method, you would have gotten a much more relevant error, because that keyword is not allowed on non-member functions.
Was This Post Helpful? 0
  • +
  • -

#5 Hulu  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 17-July 13

Re: Account Creation

Posted 17 July 2013 - 08:11 PM

I indented the function and it still isnt working. could it be that

Quote

$ctr
isn't declared earlier in the class?

View PostAtli, on 17 July 2013 - 07:40 PM, said:

Of course it does. I didn't change the code, I just altered the indentation to make the problem I was pointing out more obvious. - It's very important to make sure the code is formatter sanely. It makes a lot of otherwise hard to spot issues obvious. In your case, the fact that you put the Create_Account function at the same indentation level as the constructor makes it look like both are member functions of the Register class, while in fact the Create_Account function is being defined as a global function when instances of the Register class are created.

If you were to create two instances of the Register class, you would get a fatal error because the constructor is defining the global Create_Account function each time.

This also brings up the topic of visibility keywords. Had you specified, say, public on your Create_Account method, you would have gotten a much more relevant error, because that keyword is not allowed on non-member functions.


I indented the function and it still isnt working. could it be that $ctr isn't declared earlier in the class?
Was This Post Helpful? 0
  • +
  • -

#6 ConciselyVerbose  Icon User is offline

  • D.I.C Regular

Reputation: 90
  • View blog
  • Posts: 315
  • Joined: 05-July 13

Re: Account Creation

Posted 17 July 2013 - 08:26 PM

It's not the indentation. You need to change the brackets to close the construct method before defining the Create_Account

Like he said, the indentation doesn't change anything in php. You need the closing bracket.

This post has been edited by ConciselyVerbose: 17 July 2013 - 08:27 PM

Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2836
  • View blog
  • Posts: 9,741
  • Joined: 08-August 08

Re: Account Creation

Posted 17 July 2013 - 08:27 PM

The fact that you think indenting will change how the code works (or doesn't) shows that you need to read up on indenting, functions, and classes. Start small and work your way up, but stop thinking about this code, because you're not ready.
Was This Post Helpful? 1
  • +
  • -

#8 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3637
  • View blog
  • Posts: 5,766
  • Joined: 08-June 10

Re: Account Creation

Posted 17 July 2013 - 08:31 PM

Indenting the function won't fix it. It just makes it easier to read.

You need to remove it from the constructor; move it into the scope of the class, as opposed to having it in the scope of the class constructor.

// Bad!
class Foo {
    public function __construct() {
        function bar() {
            // ...
        }
    }
}

// Good!
class Foo {
    public function __construct() {
        // ...
    }

    public function bar() {
        // ...
    }
}



Quote

could it be that $ctr isn't declared earlier in the class?

No, that's not it. PHP variables don't need to be declared before they are used.

Honestly, I think you need to go back a step or two; go back and read up on the basics of PHP syntax and, especially, the OOP syntax. This code seems to be somewhat outside your current skill level.
Was This Post Helpful? 0
  • +
  • -

#9 ConciselyVerbose  Icon User is offline

  • D.I.C Regular

Reputation: 90
  • View blog
  • Posts: 315
  • Joined: 05-July 13

Re: Account Creation

Posted 17 July 2013 - 08:32 PM

View PostCTphpnwb, on 17 July 2013 - 08:27 PM, said:

The fact that you think indenting will change how the code works (or doesn't) shows that you need to read up on indenting, functions, and classes. Start small and work your way up, but stop thinking about this code, because you're not ready.


It's possible he knows or is used to, for example, python, where it does matter. Maybe he just borrowed the code from somewhere but if he wrote it himself some minor confusion doesn't mean he's clueless, necesarily.
Was This Post Helpful? 0
  • +
  • -

#10 Hulu  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 17-July 13

Re: Account Creation

Posted 17 July 2013 - 08:36 PM

View PostCTphpnwb, on 17 July 2013 - 08:27 PM, said:

The fact that you think indenting will change how the code works (or doesn't) shows that you need to read up on indenting, functions, and classes. Start small and work your way up, but stop thinking about this code, because you're not ready.



View PostAtli, on 17 July 2013 - 08:31 PM, said:

Indenting the function won't fix it. It just makes it easier to read.

You need to remove it from the constructor; move it into the scope of the class, as opposed to having it in the scope of the class constructor.

// Bad!
class Foo {
    public function __construct() {
        function bar() {
            // ...
        }
    }
}

// Good!
class Foo {
    public function __construct() {
        // ...
    }

    public function bar() {
        // ...
    }
}



Quote

could it be that $ctr isn't declared earlier in the class?

No, that's not it. PHP variables don't need to be declared before they are used.

Honestly, I think you need to go back a step or two; go back and read up on the basics of PHP syntax and, especially, the OOP syntax. This code seems to be somewhat outside your current skill level.

Is there somewhere that you guys would prefer that has good information about these?^^
Was This Post Helpful? 0
  • +
  • -

#11 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2836
  • View blog
  • Posts: 9,741
  • Joined: 08-August 08

Re: Account Creation

Posted 17 July 2013 - 08:46 PM

View PostConciselyVerbose, on 17 July 2013 - 11:32 PM, said:

View PostCTphpnwb, on 17 July 2013 - 08:27 PM, said:

The fact that you think indenting will change how the code works (or doesn't) shows that you need to read up on indenting, functions, and classes. Start small and work your way up, but stop thinking about this code, because you're not ready.


It's possible he knows or is used to, for example, python, where it does matter. Maybe he just borrowed the code from somewhere but if he wrote it himself some minor confusion doesn't mean he's clueless, necesarily.

I think we've seen enough to know that the probability of that is very low.

Hulu, read this topic again, and carefully. You've been given the answer, you just need to use it.
Was This Post Helpful? 0
  • +
  • -

#12 Hulu  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 17-July 13

Re: Account Creation

Posted 17 July 2013 - 08:54 PM

<?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 construct is closed before Create_Account and it still didn't work properly.
Was This Post Helpful? 0
  • +
  • -

#13 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2836
  • View blog
  • Posts: 9,741
  • Joined: 08-August 08

Re: Account Creation

Posted 18 July 2013 - 05:55 AM

Quote

...it still didn't work properly.

This is not useful information. You need to learn to ask the right questions. When you do, you'll find that you can provide some of the answers yourself. Start by telling us how it isn't working: errors provided, how it's behaving, how you think it should behave, etc.
Was This Post Helpful? 1
  • +
  • -

#14 Slice  Icon User is offline

  • sudo pacman -S moneyz


Reputation: 240
  • View blog
  • Posts: 694
  • Joined: 24-November 08

Re: Account Creation

Posted 18 July 2013 - 07:38 AM

It's possible that your error is in the MainController class. Do you have any other classes using the main controller for database access? And do they work correctly? If nothing is appearing in the database once you submit your form, then it could be your database connection or how you are executing your prepared statement.
Was This Post Helpful? 0
  • +
  • -

#15 Hulu  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 17-July 13

Re: Account Creation

Posted 18 July 2013 - 09:29 AM

View PostCTphpnwb, on 18 July 2013 - 05:55 AM, said:

Quote

...it still didn't work properly.

This is not useful information. You need to learn to ask the right questions. When you do, you'll find that you can provide some of the answers yourself. Start by telling us how it isn't working: errors provided, how it's behaving, how you think it should behave, etc.


It doesn't come up with any errors, when I hit "submit" it is supposed to enter the information from the form into the database. While doing that it is supposed to md5 and salt the login and pass together.


View PostSlice, on 18 July 2013 - 07:38 AM, said:

It's possible that your error is in the MainController class. Do you have any other classes using the main controller for database access? And do they work correctly? If nothing is appearing in the database once you submit your form, then it could be your database connection or how you are executing your prepared statement.

I have a login function and it works just fine, but it's a completely different controller. It's not the database connection, cause I have other controllers that use it.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2