-1

我有一个带有选择框的表单,并且在将行更新到表中时插入了重复项。但现在我已将选择框更改为多选框。那么如何插入多行呢?我尝试对每行使用两次 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}'
    ");
}

显然这不是正确的做法,因此,它不能按我的意愿工作。

4

1 回答 1

0

我不明白你在这里问什么。为什么你需要从另一个循环访问某些东西?我在您的查询中没有看到任何暗示这一点的内容。也许您需要将一个循环移到另一个循环中?

foreach ($customerGroupID as $cgid) {
  ...
  foreach ($penetrationGroupID as $pgid) {
    ...
  }
}
于 2012-11-09T17:31:27.517 回答