I have an array called "questions" that I am trying to INSERT into mysql with foreach. Array looks like this:
Array
(
[0] => Array
(
[booking_id] => 936
[question_id] => a
[value] => 4
)
[1] => Array
(
[booking_id] => 936
[question_id] => b
[value] => 3
)
[2] => Array
(
[booking_id] => 936
[question_id] => c
[value] => 2
)
[3] => Array
(
[booking_id] => 936
[question_id] => d
[value] => 1
)
)
FOREACH looks like this:
$sql = array();
foreach( $_POST['questions'] as $row ) {
$sql[] = '("'.$row['booking_id'].'", "'.$row['question_id'].'", '.$row['value'].')';
}
mysql_query('INSERT INTO table (booking_id, question_id, value) VALUES '.implode(',', $sql));
The foreach simply inserts the first item in the array into the table and doesn't loop through the entire array.
Any ideas where I am going wrong?
booking_idis your primary key and cannot have duplicates and every record hasid=936then that's your problem. Try adding ` or die(mysql_error())` to the end of yourmysql_query()statement.