Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
Programming Q&A
[PHP] Array Help [PHP]
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="NSA" data-source="post: 228570" data-attributes="member: 10034"><p>Hola.</p><p></p><p>So first of all, I'll show you the problem then explain.</p><p></p><p><img src="http://gyazo.com/a4c61a048623339edcca9865c6f9d049.png" alt="" class="fr-fic fr-dii fr-draggable " style="" /> </p><p></p><p>Now, you can see I'm printing out two arrays.</p><p>However, they're the exact same arrays.</p><p>For some reason, the second array doesn't match the first array.</p><p>Now, you could say that it's not really a problem, because it's working in the first array,</p><p>however it's the second array that I need the most.</p><p></p><p>If you can help me with this, it'd be appreciated.</p><p></p><p>Here's my current code:</p><p>[PHP]<?php</p><p> class user</p><p> {</p><p> public $con;</p><p> public $username;</p><p> public $password;</p><p> public $email;</p><p> public $error = array();</p><p> public $success;</p><p> public function __construct()</p><p> {</p><p> $this->con = new mysqli("localhost", "root", "", "cms");</p><p> if(isset($_POST['reg_x_21']))</p><p> {</p><p> $this->init();</p><p> $this->error = array();</p><p> }</p><p> }</p><p> public function init()</p><p> {</p><p> $this->username = $_POST['username'];</p><p> $this->password = $_POST['password'];</p><p> $this->email = $_POST['email'];</p><p> $this->init_reg();</p><p> }</p><p> public function checkUsername()</p><p> {</p><p> $username = $this->username;</p><p> $sql = "SELECT * FROM users WHERE username = ?";</p><p> if($stmt = $this->con->prepare($sql))</p><p> {</p><p> $stmt->bind_param("s", $username);</p><p> $stmt->execute();</p><p> $stmt->store_result();</p><p> if($stmt->num_rows == 1)</p><p> {</p><p> $this->error['username'] = "Username is already taken.";</p><p> var_dump($this->error);</p><p> return false;</p><p> }</p><p> else</p><p> {</p><p> $this->success['username'] = "Username is free.";</p><p> return true;</p><p> }</p><p> }</p><p> else</p><p> {</p><p> $this->error['system'] = "Database error when checking the username is already taken.";</p><p> return false;</p><p> }</p><p> }</p><p> public function checkEmail()</p><p> {</p><p> $email = $this->email;</p><p> $sql = "SELECT * FROM users WHERE email = ?";</p><p> if($stmt = $this->con->prepare($sql))</p><p> {</p><p> $stmt->bind_param("s", $email);</p><p> $stmt->execute();</p><p> $stmt->store_result();</p><p> if($stmt->num_rows == 1)</p><p> {</p><p> $this->error['email'] = "An account with this e-mail already exists.";</p><p> return false;</p><p> }</p><p> else</p><p> {</p><p> $this->success['email'] = "Email is free.";</p><p> return true;</p><p> }</p><p> }</p><p> else</p><p> {</p><p> $this->error['email'] = "This email already belongs to an account";</p><p> return false;</p><p> }</p><p> }</p><p> public function checkPassword($password)</p><p> {</p><p> $password = $this->password;</p><p> if($password)</p><p> {</p><p> return true;</p><p> }</p><p> else</p><p> {</p><p> $this->error['password'] = "Password error.";</p><p> return false;</p><p> }</p><p> }</p><p> public function init_reg()</p><p> {</p><p> $username = $this->checkUsername();</p><p> $email = $this->checkEmail();</p><p> $password = $this->checkEmail();</p><p> if($username == true)</p><p> {</p><p> if($email == true)</p><p> {</p><p> if($password == true)</p><p> {</p><p> $this->register();</p><p> }</p><p> else</p><p> {</p><p> return $this->error['password'];</p><p> }</p><p> }</p><p> else</p><p> { </p><p> return $this->error['email'];</p><p> }</p><p> }</p><p> else</p><p> {</p><p> return $this->error['username'];</p><p> }</p><p> }</p><p> public function register()</p><p> {</p><p> if($this->error == null)</p><p> {</p><p> $username = $this->username;</p><p> $password = $this->password;</p><p> $groupID = 1;</p><p> $email = $this->email;</p><p> $registered = date("d/m/Y");</p><p> $stmt = $this->con->prepare("INSERT INTO users (username, groupid, password, email, registered) VALUES (?, ?, ?, ?, ?)");</p><p> $stmt->bind_param("sisss", $username, $groupID, $password, $email, $registered);</p><p> $stmt->execute();</p><p> $this->success = "Thanks for signing up, " . $username;</p><p> }</p><p> else</p><p> {</p><p> return $this->error;</p><p> }</p><p> }</p><p> }</p><p> $user = new user();</p><p>?>[/PHP]</p><p></p><p>It's all a bit messy at the moment, but don't worry about that.</p><p></p><p>Thanks!</p></blockquote><p></p>
[QUOTE="NSA, post: 228570, member: 10034"] Hola. So first of all, I'll show you the problem then explain. [IMG]http://gyazo.com/a4c61a048623339edcca9865c6f9d049.png[/IMG] Now, you can see I'm printing out two arrays. However, they're the exact same arrays. For some reason, the second array doesn't match the first array. Now, you could say that it's not really a problem, because it's working in the first array, however it's the second array that I need the most. If you can help me with this, it'd be appreciated. Here's my current code: [PHP]<?php class user { public $con; public $username; public $password; public $email; public $error = array(); public $success; public function __construct() { $this->con = new mysqli("localhost", "root", "", "cms"); if(isset($_POST['reg_x_21'])) { $this->init(); $this->error = array(); } } public function init() { $this->username = $_POST['username']; $this->password = $_POST['password']; $this->email = $_POST['email']; $this->init_reg(); } public function checkUsername() { $username = $this->username; $sql = "SELECT * FROM users WHERE username = ?"; if($stmt = $this->con->prepare($sql)) { $stmt->bind_param("s", $username); $stmt->execute(); $stmt->store_result(); if($stmt->num_rows == 1) { $this->error['username'] = "Username is already taken."; var_dump($this->error); return false; } else { $this->success['username'] = "Username is free."; return true; } } else { $this->error['system'] = "Database error when checking the username is already taken."; return false; } } public function checkEmail() { $email = $this->email; $sql = "SELECT * FROM users WHERE email = ?"; if($stmt = $this->con->prepare($sql)) { $stmt->bind_param("s", $email); $stmt->execute(); $stmt->store_result(); if($stmt->num_rows == 1) { $this->error['email'] = "An account with this e-mail already exists."; return false; } else { $this->success['email'] = "Email is free."; return true; } } else { $this->error['email'] = "This email already belongs to an account"; return false; } } public function checkPassword($password) { $password = $this->password; if($password) { return true; } else { $this->error['password'] = "Password error."; return false; } } public function init_reg() { $username = $this->checkUsername(); $email = $this->checkEmail(); $password = $this->checkEmail(); if($username == true) { if($email == true) { if($password == true) { $this->register(); } else { return $this->error['password']; } } else { return $this->error['email']; } } else { return $this->error['username']; } } public function register() { if($this->error == null) { $username = $this->username; $password = $this->password; $groupID = 1; $email = $this->email; $registered = date("d/m/Y"); $stmt = $this->con->prepare("INSERT INTO users (username, groupid, password, email, registered) VALUES (?, ?, ?, ?, ?)"); $stmt->bind_param("sisss", $username, $groupID, $password, $email, $registered); $stmt->execute(); $this->success = "Thanks for signing up, " . $username; } else { return $this->error; } } } $user = new user(); ?>[/PHP] It's all a bit messy at the moment, but don't worry about that. Thanks! [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Programming Q&A
[PHP] Array Help [PHP]
Top