1

Code used for processing form $_POST submissions is working well on most forms but suddenly broke on a new set of forms. I can't see any difference in the forms themselves as it's based purely on the posted values and I have it fixed but I am curious why the sudden problem.

There are some cases where specific post values are not to be processed and those, when they are not needed, are in $RemoveFields as a comma-separated list which is converted to an array and on the one set of forms, it doesn't matter if $RemoveFields has any value or not but on the other set it crashes when empty.

By adding a conditional I was able to make it work but can anyone tell me what the problem is on the original code? Both the old and new are below. The first works on only some of the forms while the second seems to work on all.

The original code:

// Remove unneeded fields specified in $RemoveFields variable
if (isset($RemoveFields) && !is_array($RemoveFields)) $RemoveFields = array($RemoveFields);
$filteredarray = array_diff_key($_POST, array_flip($RemoveFields));

The same code but with a conditional for the $filteredarray value:

// Remove unneeded fields specified in $RemoveFields variable
if (isset($RemoveFields) && !is_array($RemoveFields)) $RemoveFields = array($RemoveFields);
$filteredarray = (isset($RemoveFields)) ? array_diff_key($_POST, array_flip($RemoveFields)) : $_POST;
3
  • 2
    If $RemoveFields is a comma-separated value, shouldn't you use explode() to turn it into an array? Commented Jan 26, 2019 at 0:57
  • 2
    If $RemoveFields is not set, the second line will try to do array_flip(null), which is invalid. Commented Jan 26, 2019 at 0:59
  • @Barmar This was only a snippet of a much, much longer function and possibly simplified here a bit too much but the description indicated that $RemoveFields is ultimately an array if it has any value at all. As for array_flip(null) not working, that makes sense which is why the question as some forms where $RemoveFields has no value, which is most of them, it still works. Can it be that array_flip is getting something other than null, such as 0 and if that's the case, would it still throw errors? Commented Jan 26, 2019 at 7:48

1 Answer 1

0

In the original code, you call array_flip($RemoveFields) even when $RemoveFields is not set. This fails because the argument to array_flip() must be an array.

You should use isset() to protect both lines of code:

if (isset($RemoveFields)) {
    if (!is_array($RemoveFields)) {
        $RemoveFields = array($RemoveFields);
    }
    $filteredarray = array_diff_key($_POST, array_flip($RemoveFields));
} else {
    $filteredarray = $_POST;
}
Sign up to request clarification or add additional context in comments.

7 Comments

Yes, that's essentially what I showed in the second version but the main question is why the original code works with no errors when $RemoveFields has no value, which is most of the forms. The question wouldn't have even come up if not for a set of new forms that throw errors when none of the original forms do. I should clarify that these forms are being created in an unconventional way with the first being generated using an array of values and the second (the ones with the problem) being generated by an identical array but with its source from a table in the database.
$RemoveFields is always set but rarely has any value other than in the few places where it's needed. No matter, I think I have the general idea of why this suddenly failed and that was the point of the original question. Thank you.
I'd have to see how you're setting (or not setting) $RemoveFields. Note that a variable is considered set if it's set to an empty string. If this comes from a text field in a form, it will always be set.
In one set of forms, it is a value on the form page; in the other set, it is a value in a database column. Anyway, it’s working now.
You might consider using if (!empty($RemoveFields)) rather than if(isset($RemoveFields)). This will be false for an empty string or array as well, not just an unset variable.
|