开发者

Undefined index errors (PHP)

I'm getting notices of an undefined index "id" on lines 3, 6 and 7 in this code. I can't figure out what I'm doing wrong:

    if ( isset($_POST['action']) && $_POST['action'] == 'save' ) {
    foreach ($options as $value) {
        if(($value['type'] === "checkbox" or $value['type'] === "multiselect" ) and is_array($_REQUEST[ $value['id'] ]))
            { $_REQUEST[ $value['id'] ]=implode(',',$_REQUEST[ $value['id'] ]); //This will take from the array and make one string
            }
        $key = $value['开发者_如何学编程id'];  
        $val = $_REQUEST[$key];
        $settings[$key] = $val;
    }

I'm thinking this is a small thing to fix but haven't had any luck with anything I've tried. One thing I did do is run var_dump($key) and sometimes $key is null and sometimes it's not. So I'm assuming it has to do with this.

This is part of an options page for a WordPress theme, by the way. This is the code that runs as part of the "save" function.

The script actually works properly if debug mode is turned off, but when debug mode is on, these notices pop up when hitting the save button on the options page. Thanks in advance for any insights, and let me know if I need to post more code to give more context.

EDIT: I've posted the options page in pastebin. It's long. It contains t13lo's fixes. The problem code is on line 1957. Thanks: http://pastebin.com/NGX6qzgr


You can change line 3 to:

if ( ($value['type'] === "checkbox" || $value['type'] === "multiselect" ) && isset($_REQUEST['id']) && is_array($_REQUEST[ $value['id'] ]) )

However, the rest of your code in your foreach seems dependent upon $value['id']. This value is not always being set so it may make these lines:

$key = $value['id'];  
$val = $_REQUEST[$key];
$settings[$key] = $val;

Irrelevant if $value['id'] is not set. If that's the case, placing all of the code that is currently in the foreach within an if (isset($value['id'])) { may be appropriate.

You'll need to further analyze and understand what the code is (and needs to be) doing in order to determine the correct logic.


Have no idea what your code is suppose to be doing, but based on what you've posted give this a shot.. (replacing obviously just the foreach loop part of your code)..

foreach ($options as $value) {
    if( !isset( $value['id'] ) )
        continue;
    if( !isset( $_REQUEST[$value['id']] ) ) {
        $settings[$value['id']] = '';
        continue;
    }
    if( $value['type'] === "checkbox" || $value['type'] === "multiselect" ) { 
        // No isset check here, the top conditional catches non-set items
        if( is_array( $_REQUEST[$value['id']] ) )
            $_REQUEST[$value['id']] = implode( ',', $_REQUEST[$value['id']] );
    }
    $settings[$value['id']] = $_REQUEST[$value['id']];
}

Should do exactly the same as before, with exception to checking vars are set first, at also setting a value in the $settings array to an empty string when the matching $_REQUEST var isn't set.. (assuming i'm following what the code is doing correctly that is)..

EDIT: Following discussion in comments i've updated the code above, that should fix the notice on line 1957. It makes sense for that key not to be set for your code, not all the array items in $options are actually options, some refer to headings/tabs(whatever) that get output on your theme's options page(so it makes sense to skip them in the loop above).

I think that will solve your problem, but having said that i think it's important to point out that the code in that function is not ideal, the whole saving mechanism doesn't look very well sanitized overall. I'd be inclined to suggest setting the required capability for that page to manage_options so it's restricted to administrators at least until such time a replacement can be put in place(suggestions for which should be in a seperate question, and ideally on WPSE).


try this :

if(($value['type'] === "checkbox" || $value['type'] === "multiselect" ) &&  isset($value['id']) && is_array($_REQUEST[ $value['id'] ])){ ...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜