带有更新查询的未定义偏移通知

I have a form that displays a Selection ID, Selection, and Definition pairing from a database. The form is dynamic...depending on the user, there could be any number of these pairings. Here is an example of three pairings:

Selection ID, Selection, Definition

Selection ID, Selection, Definition

Selection ID, Selection, Definition

The page displays just fine, but if the user wants to edit a Selection or Definition, I receive the following error when the form is submitted (Note: Line 41 is my Update query):

"Notice: Undefined offset: 3 in (link to my php file) on line 41"

I assume that the notice is telling me that the query is not reading the information in my three arrays...but I don't know what I should be putting in my query so it will read properly. Thank you very much for your help.

if(isset($_POST['submit'])){    
    $selection_id = array();
    $selection = array();
    $definition = array();

foreach ($_POST as $key => $value){     
    // If array variable starts with "pd_selection_id_for_" save to $selection_id array, otherwise continue to the next array.  

    if(strpos($key, 'pd_selection_id_for_') !== false){
        $selection_id[] = mysql_real_escape_string($value);
    }else if(strpos($key, 'selection_for_') !== false){
        $selection[] = mysql_real_escape_string($value);
    }else if(strpos($key, 'definition_for_') !== false){
        $definition[] = mysql_real_escape_string($value);
    }
}

// Count one of the arrays to select the paired fields and update the database.

$total = count($definition);

for ($i=1; $i <= $total; $i++){
    // Update query for the paired selections and definitions.
    $query = mysql_query("UPDATE `pd_selections` SET `pd_selection` = '$selection[$i]', `pd_definition` = '$definition[$i]' WHERE `pd_selection_id` = '$selection_id[$i]' ") or die(mysql_error());
}

}

Minor syntax error the fix for Line 41 is below:

$query = mysql_query("UPDATE `pd_selections` SET `pd_selection` = '".$selection[$i]."', `pd_definition` = '".$definition[$i]."' WHERE `pd_selection_id` = '".$selection_id[$i]."'") or die(mysql_error());

Undefined Offset error means that the position that you are trying to access in your array is not set and therefore its not defined.

Problem you have is that your arrays are not initialized at the index you are trying to access from loop, try this:

$i = 0;
foreach ($_POST as $key => $value){     
   //initialize array at position $i
   $selection_id[$i] = $selection[$i] = $definition[$i] = '';

   if(strpos($key, 'pd_selection_id_for_') !== false){
       $selection_id[$i] = mysql_real_escape_string($value);
   }else if(strpos($key, 'selection_for_') !== false){
       $selection[$i] = mysql_real_escape_string($value);
   }else if(strpos($key, 'definition_for_') !== false){
       $definition[$i] = mysql_real_escape_string($value);
   }

   $i++;
}

this way you know that all your arrays size are same and therefore will be set on same positions that other arrays are set.