我试图让这个嵌套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
它在每次迭代时不断给我数组中的第一个值。我该如何解决?
这是您要求的数组。
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')