neoszion
neoszion

Reputation: 249

html form array for each update into mysql

I have been racking my brains trying to figure this out for days and have come up short. I am trying to update rows in the database with the following form value:

<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"]),'" />

This following array back:

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

Now im trying to update into a database with the following but to no avail I can only update the last row of fields (the [1] values) :( any help would be great

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
}

Upvotes: 2

Views: 502

Answers (2)

Doa
Doa

Reputation: 2015

Something like this?

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

Upvotes: 1

Peon
Peon

Reputation: 8020

Add sub-array numbers too.

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
}

Upvotes: 3

Related Questions