2

几天来,我一直在绞尽脑汁想弄清楚这一点,但最终还是失败了。我正在尝试使用以下表单值更新数据库中的行:

<input type="text" name="item[]" maxlength="255" value="',htmlentities($item["item"]),'">
<input type="text" name="description[]" maxlength="255" value="',htmlentities($item["description"]),'">
<input type="text" name="rate[]" maxlength="10" value="',htmlentities($item["rate"]),'">
<input type="hidden" name="itemid[]" value="',htmlentities($item["id"]),'" />

以下数组返回:

Array
(
[item] => Array
    (
        [0] => item listing 1
        [1] => item listing 2
    )

[description] => Array
    (
        [0] => item testing description
        [1] => item testing description
    )

[rate] => Array
    (
        [0] => 1.00
        [1] => 2.00
    )

[itemid] => Array
    (
        [0] => 1
        [1] => 2
    )
)

现在我尝试使用以下内容更新到数据库中,但无济于事,我只能更新最后一行字段([1] 值):(任何帮助都会很棒

if (is_array($values))
{
for ($i = 0; $i < count($values); $i++)
{
    $item           = $values['item'];
    $description    = $values['description'];
    $rate           = $values['rate'];
    $id             = $values['itemid'];

    // $query = "UPDATE `invoice_items` SET `item` = '{$item}', `description` = '{$description}', `rate` = '{$rate}' WHERE `id` = '{$id}';";
    // Setting query function here
}
4

2 回答 2

3

也添加子数组编号。

if (is_array($values))
{
for ($i = 0; $i < count($values); $i++)
{
    $item           = $values['item'][$i];
    $description    = $values['description'][$i];
    $rate           = $values['rate'][$i];
    $id             = $values['itemid'][$i];

    // $query = "UPDATE `invoice_items` SET `item` = '{$item}', `description` = '{$description}', `rate` = '{$rate}' WHERE `id` = '{$id}';";
    // Setting query function here
}
于 2012-07-18T16:40:59.183 回答
1

像这样的东西?

if (is_array($values)) {
    for ($i = 0; $i < count($values['itemid']); $i++) {
        $item           = $values['item'][$i];
        $description    = $values['description'][$i];
        $rate           = $values['rate'][$i];
        $id             = $values['itemid'][$i];

        $query = "UPDATE `invoice_items` SET `item` = '{$item}', `description` = '{$description}', `rate` = '{$rate}' WHERE `id` = '{$id}';";
        mysql_query($query);
    }
}
于 2012-07-18T16:42:45.087 回答