views:

241

answers:

6

below is my register form for my game and it is looking for errors that the user may have done, but even if an error is found it wont add it onto the $errors array. When I print_r the array it returns empty.

I believe something is going wrong with the if functions because if I add a value into the array from outside one of the if functions it adds it.

Here is my code:

if (isset($_GET['action'])){
db_connect();
db_select();

if ($_GET['action'] == "register"){
  $username = $_POST['username'];
  $password = $_POST['password'];
  $confirm = $_POST['confirm'];
  $email = $_POST['email'];
  $agree = $_POST['agree'];

  $errors = array();
  if (!isset($username)){
    $errors['0'] = "You did not specifiy a username";
  }elseif (ereg("[^a-z0-9]", $username)) {
    $errors_array['0'] = "Usernames can only contain lowercase letters and numbers";
  }elseif (mysql_num_rows(mysql_query("SELECT username FROM users WHERE username = '{$username}'")) > 0) {
    $errors['0'] = "The username you chose has already been taken";
  }

  if (!isset($password)){
    $errors['1'] = "You did not specify a password";
  }elseif ($password != $confirm){
    $errors['1'] = "The password and password confirm fields do not match";
  }

  if (!isset($email)){
    $errors['2'] = "You did not specify a E-mail address";
  }elseif (mysql_num_rows(mysql_query("SELECT email FROM users WHERE email = '{$email}'")) > 0) {
    $errors['2'] = "The E-mail you specified is already being used";
  }
  print_r($errors);
  }
}
+1  A: 

try adding a test case to your array on creation.

$errors = array('test'=>'Test');

Then if you receive this output only when you print_r you know your if statements arent working as expected.

Without more information about what values the variables have on entry its difficult to debugs.

Maybe do a dump of $_POST (print_r($_POST)) to make sure you are getting the values you think you are.

Toby Allen
+2  A: 

Well, the first thing I'd do is not address your arrays by key. If the only error is no email address then do you really want the only error to be indexed by '2'?

Better to user $error[] = 'Something...";

Visage
+5  A: 

Some quick points:

  • In one place you use $errors_array instead of $errors
  • Instead of using indexes when writing to the array, use $errors[] = "some_error". This will add some_error to the end of the array.
gnud
+1  A: 

In one place there you are calling $errors_array['0'], you should add into the array by doing $errors[] = "Foo";

2nd please look into Safe SQL

3rd check what if statements are running. Place echo "1"; into each of your if statements (and increment that number by 1 for each placement. Then you can see what is actually running. (if at all))

Ólafur Waage
+8  A: 

First you do this:

$username = $_POST['username'];
$password = $_POST['password'];
$confirm = $_POST['confirm'];
$email = $_POST['email'];
$agree = $_POST['agree'];

And then you check if $username, $password, etc isset(). This will always return true (because you just initialized them). You should do the if(isset()) on the $_POST variables (I.E. not the variables you created).

Because of this all your checks will work perfectly, because you don't do any checks that check the actual data (valid e-mail address, min / max username length, etc).

A simple way to discover problems like this is to set error_reporting to E_ALL. This will show all errors / warnings / notices / etc. This will give you a warning if you use a variable that does not exist / has not been set.

Bart S.
A: 

Solved my own problem, I should have been using "empty" not "!isset" in my if functions. because they were set as below shows:

$username = $_POST['username'];
$password = $_POST['password'];
$confirm = $_POST['confirm'];
$email = $_POST['email'];
$agree = $_POST['agree'];

Thanks for the help anyway.

Ryan
This is either an addendum (edit) to the question or a comment on the solution/question. But definitely not an answer.
soulmerge