Jump to content

Insert Multiple Arrays


mj9999

Recommended Posts

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

}

}

}

Link to comment
https://forums.phpfreaks.com/topic/142979-insert-multiple-arrays/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.