ceci Posted April 9, 2010 Share Posted April 9, 2010 I am trying manipulate multiple rows to add/update/delete database. Right now, the first time it INSERTS all rows correctly. During Update however, it re-enters previous values plus any new rows. How can I stop this behavior? SUBMISSION FORM. <FORM><? php // Make sure the array has items in it. Display it only if there are items. // "$array_items contains items from database. if(count($array_items) != 0) { foreach($array_items as $className => $sub_className){ ?> <tr> <!-- Display Class --> <td rowspan="<?php print $rowspan_no+1; ?>"> <?php print $className; ?> </td> <!-- Display subclass --> <td rowspan="<?php print $rowspan_no+1; ?>"> <?php print $sub_className; ?> </td> <?php foreach($item as $item_key => $item_name){ ?> <?php $m_rows = mysql_fetch_array($m_qry);?> <tr id='r_id<?=++$count?>_box'> <!-- Display the Name --> <td><?php print $item_name;?> <?php // IMPORTANT*** THE NEXT 3 HIDDEN FIELDS ARE TO BE USED TO ADD OR UPDATE NEW ROWS?> // This field contains an ID if there was a previous entry. <input name="main_item_id[<?=$count?>]" type="hidden" value="<?=$m_rows['id']?>" > // This field contains IDs from Database for all rows for data entry. Thus will always have an ID. and an X number of items. <input name="secondary_id[<?=$count?>]" type="hidden" value="<?=$secondary_id?>" > <input name="sub_className[<?=$count?>]" type="hidden" id="uniqueid<?=$count?>" value="<?=$sub_item;?>"> </td> // user will enter data here <td><input type="text" name="duration[<?=$count?>]" value="<?=$m_rows['duration']?>" size="3"></td> // user will enter data here <td><input type="text" name="effect[<?=$count?>]" value="<?=$m_rows['effect']?>" size="3"></td> <td> </td> <?php } ?> </tr> </form> HERE IS MY INSERT and UPDATE code. The first time it INSERTS all rows correctly. During Update however, it re-enters previous values plus any new rows. How can I stop this behavior? // count($secondary_id) represents the number of rows the form has automatically generated. Eg 10 rows. for ($i = 1; $i <= count($secondary_id); $i++) { // check to see if the id exists. if($secondary_id[$i]) { // Presence of 'main_item_id' represents previous entry. if so, update it. if(!empty($main_item_id)){ $qry = "UPDATE table SET effects='$effects[$i]', duration='$duration[$i]' WHERE id='$main_item_id[$i]'"; $result = mysql_query($qry); if ($result) { $count++; } echo $count .' items updated.'; // if no 'main_item_id' exists, then insert. // This is giving me duplicates! Meaning, re-enters whatever was previously entered + the new entries. }else { // there is no 'main_item_id' so Do INSERT. if($duration[$i]){ echo 'insert items'; $count =0; $insert_qry = "INSERT INTO table(duration, effects) VALUES('$duration[$i]','$effects[$i]')"; $result = mysql_query($insert_qry); if ($result) { $count++; } echo $count .' items inserted.'; } } } Thanks again. C Link to comment https://forums.phpfreaks.com/topic/198156-inserting-and-updating-multiple-rows/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.