Scott B
Scott B

Reputation: 40157

exception handling for undefined index error?

The script below is called when saving my app's options page. All options are stored in an array in $options.

I'm getting a debug error "undefined index, id" on the line with the comment below. Any ideas how I can fix the script?

        foreach ($options as $value) 
        {
            if( isset( $value['id'] ) && isset( $_REQUEST[$value['id']] ) ) 
            {
                update_option( $value['id'], stripslashes($_REQUEST[$value['id']])  ); 
            }
            else
            {
                update_option( $value['id'], ""); //Error Here
            }
        }

Upvotes: 1

Views: 2327

Answers (1)

rockerest
rockerest

Reputation: 10508

Your if{} segment precludes the code in your else{} segment from working.

In other words:

In your if block, you ask: "Does $value['id'] exist?"

If it does not, your code executes your else block, which then attempts to reference the non-existent variable.

You'll need to set the array key before you can update it.

Your update_option function should simply check to see if the variable exists, and set it instead of updating it, if it does not.

Upvotes: 2

Related Questions