2

I have the following array

Array ( [productid] => 3
[newid] => 3
[name] => 
[description] => 
[price] => 
[onlinestock] => 
[weight] => 
[keywords] => 
[longdescription] => 
[graphic1] => 89.jpeg 
[graphic2] => 
[parameter1] => 
[parameter2] => 
[category1id] => 101 
[category2id] => 
[category3id] => 
[category4id] => 
[category5id] => 
[extra1] => 
[extra2] => 
[extra3] => 
[group1id] => 1575 
[groupprice1] => 0 
[group2id] => 1576 
[groupprice2] => 0 
[group3id] => 1577 
[groupprice3] => 0 
[group4id] => 1578 
[groupprice4] => 0 
[group5id] => 1579 
[groupprice5] => 0 

I'm doing the following to check for empty values

 if (empty($_REQUEST["category1id"])){ 
                $_REQUEST["category1id"] = 0;
        }else if (empty($_REQUEST["category2id"])){
                $_REQUEST["category2id"] = 0;
        }else if (empty($_REQUEST["category3id"])){
                $_REQUEST["category3id"] = 0;
        }else if (empty($_REQUEST["category4id"])){
                $_REQUEST["category4id"] = 0;
        }else if (empty($_REQUEST["category5id"])){
                $_REQUEST["category5id"] = 0;
        }else if (empty($_REQUEST["price"])){
                $_REQUEST["price"] = 0;
        }else if (empty($_REQUEST["onlinestock"])){
                $_REQUEST["onlinestock"] = 0;
        }else if (empty($_REQUEST["weight"])){
                $_REQUEST["weight"] = 0;
        }

but somehow it's not detecting any of these values as empty? I'm not sure what this could be I tried $_REQUEST['category1id'] == "" and that also is not working. Any help would be appreciated

2
  • 4
    If you are only looking for POST variables use $_POST, not $_REQUEST. Commented Jan 27, 2011 at 13:59
  • 2
    look on : php.net/manual/en/function.empty.php .The following things are considered to be empty: * "" (an empty string) * 0 (0 as an integer) * "0" (0 as a string) * NULL * FALSE * array() (an empty array) * var $var; (a variable declared, but without a value in a class) Commented Jan 27, 2011 at 14:01

4 Answers 4

7

Your logic is flawed. By using else if, only the conditions will match where the previous condition returned false. Get rid of each else and it should work.

Sign up to request clarification or add additional context in comments.

1 Comment

true fixed it, and it worked, silly from me to have had the else there ;-) Thanx
4

Pekka is 100% correct. But I think it would be easier to change your approach. Replace your entire if/else block with this loop:

foreach ($_POST as &$post) {
    $post = empty($post) ? 0 : $post;
}

1 Comment

btw, for those that do not know what the $post = empty(.... is, it is a Ternary Operator (php.net/manual/en/language.operators.comparison.php)
2

Good comments above, main thing is don't use $_REQUEST:

$_REQUEST contains: $_COOKIE, $_GET, and $_POST variables

if you use $_REQUEST you have no guarantee that the data came from the POST, which leads to security holes in your script. Explicitly declare what you want $_POST/$_GET.

Also read the empty() fn documentation

Take a look at isset() as well -- (Determine if a variable is set and is not NULL):

Comments

0

I think you might want to dwell into what is going on there. Sometime times for what you were trying to do I've used isset. The else if answer should be taken into account.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.