tags:

views:

48

answers:

3

Here is my code:

if (isset($_POST['addmonths'])){
    if (!empty($_POST['months'])){
        if (is_numeric($_POST['months'])){
            $monthstoadd = $_POST['months'];
            if ($monthstoadd < 1){
                mysql_query("UPDATE users SET months='lifetime' WHERE username='$lookupuser'");
                echo "Successfully set " . $lookupuser . " to lifetime";
            }elseif ($monthstoadd > 0){
                $monthstoadd = $monthstoadd*2592000;
                mysql_query("UPDATE users SET months=months+'$monthstoadd' WHERE username='$lookupuser'");
                echo "Successfully added " . $_POST['months'] . " months to " . $lookupuser . "'s paid time.";
            }else{
                echo "Error.";
            }
        }else{
            echo "Months need to be numeric. If you're trying to set lifetime, use 0.";
        }
    }else{
        echo "You didn't enter anything.";
    }
}

When I enter 0, it should set the user to lifetime, but instead it just echos You didn't enter anything. Not really sure how to fix this. Any ideas?

+1  A: 

In general, You can substitute empty for isset (which returns false only of the variable doesn't exist or is null) or array_key_exists (for array keys).

In this case, you can do

if (!isset($_POST['months']) || $_POST['months'] === "")

Alternatively

if (!array_key_exists('months', $_POST) || $_POST['months'] === "")

Since this is POST data, null will never be a value (the browser can either not send the input or it sends an empty string, which PHP translates to ""). Therefore, isset and array_key_exists are interchangeable. isset is, however, preferred, since it's not a function and therefore is faster to run (not mention it's faster to write it).

Artefacto
+2  A: 

Entering 0 into a field registers as empty?

Yes, it does. From the docs for empty:

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)

Any ideas?

empty doesn't seem like a good match for the condition you're checking for (did they type something in). Since you're already apparently using a marker field to indicate that there should be a value there, you might consider trim and a check against a blank string:

if (trim($_POST['months']) != ''){

Since you'r using $_POST['months] in four different places, I'd probably also cache the trimmed version to a local variable.

T.J. Crowder
Thanks for the information, but that doesn't answer the question.
Rob
To extend on this answer, include a check to see if the value is exactly 0: `if (!empty($_POST['months']) || ($_POST['months'] === 0)){`
Joseph
@Rob: It answers the question you asked in your title, which is meant to summarize your overall question (I took the "Any ideas?" at the end to mean "any ideas why `empty` isn't working?"). I've added something to address how you might fix it.
T.J. Crowder
A: 

You need to check the type to make sure it is empty instead of registers as empty with ===.

if ($_POST['months'] !== ''){

This checks to see if it exactly equivalent to empty. If it isn't it passes.

Aaron Harun