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());
}
}