2

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
}

2 Answers 2

3

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
}
Sign up to request clarification or add additional context in comments.

2 Comments

yep, that should do. you could use php's foreach as well
thanks works like a charm... very quick response to., very impressed.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.