我試圖讓這個嵌套的foreach
循環工作,但我沒有運氣。這是我的代碼。嵌套的foreach循環問題
$q = 0;
$arrayCountTwo = count($_POST['quantity']);
$i = 0;
$arrayCountThree = count($_POST['items']);
foreach ($_POST['items'] as $items) {
$sql = '';
foreach ($_POST['quantity'] as $quantity) {
$q++;
if ($q > $arrayCountTwo) {
break;
} else {
$sql .= "INSERT INTO `trade_show_reserved` (ProductID, DateReserved, DateReservedEnd, QuantityReserved) VALUES ('".$items."','".$startDate."', '".$endDate."','".$quantity."')";
}
var_dump($sql);
}
}
它每次迭代都會給我$items
數組中的第一個值。我該如何解決?這是您請求的數組。
項目數組和數量數組。
array(3) {
[0]=>
string(2) "11"
[1]=>
string(1) "6"
[2]=>
string(1) "2"
}
array(3) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
[2]=>
string(1) "1"
}
每次都應該這樣做。
INSERT INTO `ts_table` (ProductID, DateReserved, DateReservedEnd, QuantityReserved) VALUES ('11','2013-4-11', '2013-4-25','1')
INSERT INTO `ts_table` (ProductID, DateReserved, DateReservedEnd, QuantityReserved) VALUES ('6','2013-4-11', '2013-4-25','2')
在你做任何事之前,看看你的代碼中看起來有什麼大的SQL注入漏洞。 – 2013-03-26 21:24:30
我知道的sql問題。我只想讓循環先在我的本地環境中工作。 – wowzuzz 2013-03-26 21:25:17
向我們展示你的'items'和'quantity'數組? – MichaelRushton 2013-03-26 21:27:57