Jump to content

Insert into table from array


Rachel
 Share

Recommended Posts

I am trying to insert records from an foreach array, it only inserts the first record and I cannot work out how to get it to loop through all of the records.

 

Thank you

 

foreach($_SESSION['post_data_array'] AS $seat) {
     $rowId = substr($seat, 0, 1);
     $columnId = substr($seat, 1);
      echo $rowId . $columnId . ", "; 
}

$sql125 = "INSERT INTO booked_seats(booking_id, row_id, column_id) values ('$book_id', '$rowId', '$columnId')";
$result125 = mysql_query($sql125);
if ($result125)
	{
		echo "worked";
	}
else
	{
		echo "didnt work";
	}			

Link to comment
Share on other sites

Try something like: (Pik's suggestion)

foreach($_SESSION['post_data_array'] AS $seat) {
     $rowId = substr($seat, 0, 1);
     $columnId = substr($seat, 1);
      echo $rowId . $columnId . ", "; 
  $sql[] = "('$book_id', '$rowId', '$columnId')";
}

$sql125 = "INSERT INTO booked_seats(booking_id, row_id, column_id) values " . implode(',',$sql);

Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

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