Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.