我有一個名爲「問題」,我試圖INSERT到MySQL與foreach的數組。陣列看起來像這樣:PHP與foreach的foreach
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看起來像這樣:
$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));
在foreach只需插入數組中的第一項到表和通過整個陣列不循環。
任何想法我錯了嗎?
你確定$ _POST包含你認爲它的作用嗎? – maackle
print_r($ _ POST ['questions']);給出以上數組 – user3192581
所有booking_id是936 ..? –