我有一个带有选择框的表单,并且在重复时插入将行更新为表格。但现在我已将选择框更改为多选框。那么如何插入多行?我尝试每行使用两次foreach循环。但是这个逻辑的问题是我不能从一个数组中插入另一个foreach循环中的值。请帮忙
HTML:
//html
<select name="customer_groupings[]" multiple="multiple">
<option value="-1">-All Groups-</option>
<option value="46" selected="selected">Accessories / Retailer</option>
<option value="47" selected="selected">Counter</option>
<option value="14" selected="selected">Export</option>
<option value="45">Fast Fit</option>
<option value="2">Independent</option>
<option value="44">Internal</option>
<option value="1">National</option>
<option value="19">Other</option>
</select>
<select name="penetration_group[]" multiple="multiple">
<option value="-1">All</option>
<option value="1" selected="selected">Main Supplier</option>
<option value="2" selected="selected">Not Main Supplier</option>
<option value="3" selected="selected">New Customer</option>
</select>
// there are other form elements as well.
PHP:
// php
/*
table contact_scripts has the following fields
ID (PK)
branch
customerGroupID
penetrationGroupID
type
message
branch,customerGroupID,penetrationGroupID,type are UNIQUE
*/
foreach ($customerGroupID as $value) {
$q = $this->reporting->query("
INSERT INTO
`contact_scripts` (`branch`, `customerGroupID`, `type`, `message`)
VALUES
(-1, {$value}, {$type}, '{$message}')
ON DUPLICATE KEY UPDATE
message = '{$message}'
");
}
foreach ($penetrationGroupID as $value) {
$q = $this->reporting->query("
INSERT INTO
`contact_scripts` (`branch`, `penetrationGroupID`, `type`, `message`)
VALUES
(-1, {$value}, {$type}, '{$message}')
ON DUPLICATE KEY UPDATE
message = '{$message}'
");
}
显然,这不是正确的做法,因此,它不能按我的意愿行事。
答案 0 :(得分:0)
我不明白你在这里问的是什么。为什么你需要从另一个循环中访问某些东西?我没有在您的查询中看到任何暗示的内容。也许你需要在另一个内部移动一个循环?
foreach ($customerGroupID as $cgid) {
...
foreach ($penetrationGroupID as $pgid) {
...
}
}