每次更新到mysql的html表单数组

时间:2012-07-18 16:36:39

标签: php mysql multidimensional-array

我一直绞尽脑汁试图解决这个问题已经好几天了。我正在尝试使用以下表单值更新数据库中的行:

<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
}

2 个答案:

答案 0 :(得分: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
}

答案 1 :(得分: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);
    }
}