A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearArray always comes back as empty - page 1
User InfoPosts
Array always comes back as empty#1
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(])){

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";

posted date: 2009-04-14 02:34:00

Re: Array always comes back as empty#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-14 02:34:01

Re: Array always comes back as empty#3
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.

posted date: 2009-04-14 02:40:00

Re: Array always comes back as empty#4
Some quick points:In one place you use $errors_array instead of $errorsInstead of using indexes when writing to the array, use $errors[] = "some_error". This will add some_error to the end of the array.

posted date: 2009-04-14 02:41:00

Re: Array always comes back as empty#5
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 SQL3rd 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))

posted date: 2009-04-14 02:42:00

Re: Array always comes back as empty#6
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.

posted date: 2009-04-14 02:48:00

Re: Array always comes back as empty#7
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.

posted date: 2009-04-14 02:57:00

Re: Array always comes back as empty#8
This is either an addendum (edit) to the question or a comment on the solution/question. But definitely not an answer.

posted date: 2009-04-14 23:39:00

Re: Array always comes back as empty#9
Please use the edit link on your question to add additional information. The Post Answer button should be used only for complete answers to the question.

posted date: 2014-04-22 07:36:00

select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.