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

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

最佳答案

也添加子数组数字。

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
}

关于php - 每次更新到mysql的html表单数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11546184/

相关文章:

php - 从 Facebook 好友获取共同好友(MySQL 表)

mysql - 更新 mysql 行时(您无法在 FROM 子句中指定目标表 'x' 进行更新)

sql - 请帮助优化长时间运行的查询(左外连接,带有 2 个派生表)

matlab - 如何在 Matlab 中裁剪任意维数的矩阵?

c - 如何在 C 函数中传递二维数组(矩阵)?

php - 如何使用php在邮件中附加多个文件?

php - 为什么 PHP 中的查询内部查询比 2 个查询慢?

php - 如何更改 Joomla 表单值?

mysql - 如何将Access MDB格式数据库导入MySQL?

java - 在 Scala 中创建并填充二维数组