[PHP] Array Help [PHP]

NSA

sudo apt-get thefuckout.tar.gz
Dec 9, 2011
715
86
Hola.

So first of all, I'll show you the problem then explain.

a4c61a048623339edcca9865c6f9d049.png


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

It's all a bit messy at the moment, but don't worry about that.

Thanks!
 

NSA

sudo apt-get thefuckout.tar.gz
Dec 9, 2011
715
86
I don't understand?
It's one array, but the values seem to be changing.
I set them whenever there's an error, like in the checkUsername function.
"$this->error['username'] = "Username is already taken.";".
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
You don't really need to keep assigning keys to each item, you can just do $this->error[] = 'username taken'; and then do print_r($this->error);

In __construct you don't need to do $this->error = array(); because you've already done public $error = array();

Try doing a var_dump() of your arrays rather than return. It should give a more in depth look into it.
 

NSA

sudo apt-get thefuckout.tar.gz
Dec 9, 2011
715
86
Oh, yeah... I forgot to remove that from the construct.
I gave them keys so I can lay out the errors in separation from each other, when displaying them to the user.
I did do var_dump on line 38.
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
Oh, yeah... I forgot to remove that from the construct.
I gave them keys so I can lay out the errors in separation from each other, when displaying them to the user.
I did do var_dump on line 38.
Truth is, no user is going to want to see array(1) { [....] } etc, you could do something like this

I'm on iPad so if its bad, sorry lol

PHP:
public function displayErrors() {
    if (count($this->error) > 0) {
        echo '<ul>';
        foreach ($this->error as $anerror) {
            echo '<li>' . $anerror . '</li>';
        }
        echo '</ul>';
    }
}
 

academic

Member
Nov 17, 2011
31
6
I'm not sure if anyone has touched on your original array. However, you mentioned you didn't understand why they were always being changed. Your original followed, similar to this.

PHP:
$errors = array();
$errors["username"] = "Username is to long.";
$errors["username"] = "Username is taken.";

When you printed it, you were only getting the one error in the "username" key. This is because you were overwriting them each time you repeated the above code and not appending it.
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
What @ACADEMiC said is true, if you wanted to add multiple values to the username key, you could do:

PHP:
$errors = array();
$errors["username"][] = "Username is too long.";
$errors["username"][] = "Username is taken.";

If you do a print_r, you would get 2 items returned from the array rather than just one.
 

academic

Member
Nov 17, 2011
31
6
What @ACADEMiC said is true, if you wanted to add multiple values to the username key, you could do:

PHP:
$errors = array();
$errors["username"][] = "Username is too long.";
$errors["username"][] = "Username is taken.";

If you do a print_r, you would get 2 items returned from the array rather than just one.
Because that's appending to the "username" key! :)
 

NSA

sudo apt-get thefuckout.tar.gz
Dec 9, 2011
715
86
The thing is, I was also using a password that was > 4 characters long, which would have resulted in an error.
In fact, at the time of print screening this, I was using an already taken username, a password consisting of 1 character and an e-mail that was already in use.
Which is why I was confused as to why I was only receiving one error.
 

Users who are viewing this thread

Top