mj9999 Posted January 29, 2009 Share Posted January 29, 2009 I am trying to post multiple arrays from a form and insert into mysql. Seems like no matter what I try I either loop through and insert too much duplicate data or don't insert enough data. The example below duplicates the data. Anyway, I hope one of the really good programmers around here will show me how to really do it right. The Form Elements echo '<input type="checkbox" name="event_position[]" value="'.$position_row['id'].'"> <b>'.$position_row['name'].'</b>'; echo '</td>'; echo '<td class=main nowrap>'; echo 'Time: <input type="text" size="14" name="position_time[]">'; echo '</td>'; echo '<td class=main nowrap>'; echo 'Quantity: <input type="text" size="3" name="position_quantity[]">'; The MySQL Insert if(isset($_POST['add'])){ mysql_query("INSERT INTO events (user_id, name, location, date, notes, contacts) VALUES('$user_id', '$name', '$location', '$date', '$notes', '$contacts' ) ") or die(mysql_error()); $event_result = mysql_query("SELECT * FROM events ORDER BY id DESC LIMIT 1") or die(mysql_error()); $event_row = mysql_fetch_array( $event_result ); foreach($event_position as $position) { echo $position; foreach($position_time as $time) { echo $time; foreach($position_quantity as $quantity) { echo $quantity; $query = mysql_query("INSERT INTO event_positions (event_id,position_id,time,quantity)VALUES ('".$event_row['id']."','$position','$time',$quantity)")or die(mysql_error()); } } } Quote Link to comment https://forums.phpfreaks.com/topic/142979-insert-multiple-arrays/ Share on other sites More sharing options...
fenway Posted January 29, 2009 Share Posted January 29, 2009 I have no idea what you mean... but you can use a multi-valued insert statement. Quote Link to comment https://forums.phpfreaks.com/topic/142979-insert-multiple-arrays/#findComment-749764 Share on other sites More sharing options...
mj9999 Posted January 29, 2009 Author Share Posted January 29, 2009 If someone could please take a look at what I have and give some details on how to do it right I would really appreciate it. Basically, I have some experience at submitting an array to one field. But I have never done more Thanks Quote Link to comment https://forums.phpfreaks.com/topic/142979-insert-multiple-arrays/#findComment-749911 Share on other sites More sharing options...
fenway Posted February 2, 2009 Share Posted February 2, 2009 Do your echo statements produce the expected tuples in the outptu? Quote Link to comment https://forums.phpfreaks.com/topic/142979-insert-multiple-arrays/#findComment-752438 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.