Jump to content

Insert into table from array


Rachel

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
https://forums.phpfreaks.com/topic/228520-insert-into-table-from-array/
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);

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.