Update query not executing as expected

In a update form I use the following foreach loop to display multiple items in textfields:

<?php foreach ( $pakket_items as $item ): ?>
    <input type="text" name="items[]" class="form-control" value="<?= $item['item']; ?>" placeholder="Pakketitem" tabindex="5">
<?php endforeach; ?> 

In the controller I use a foreach loop as well to actually make the update happen:

$pakket_id            = filter_input(INPUT_POST, 'pakket_id', FILTER_SANITIZE_NUMBER_INT);
$items              = filter_input(INPUT_POST, 'items', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
foreach ( $items as $item )
{
    $update    =    $this->pakketten->pakket_items_wijzigen($pakket_id, $item);    
}

But instead that the right fields are updated all rows in the database are set to the value of the last item. What am I doing wrong?

Thank you in advance

What am I doing wrong?

You are asking strangers who have no idea what’s wrong with your code instead of debugging it.

First of all you have to narrow the problem to the smallest code possible, means there should be no input, no POST, no pakket_items_wijzigen, no pakketten but just a hardcoded array of values and a code to update.

<input type="text" name="items[]" class="form-control" value="<?= $item['item']; ?>" placeholder="Pakketitem" tabindex="5">

I’ll give you where you are going wrong, but it’s up to you to fix it.

names=“items” in your input tag should be something like names=“items[index_name]” that is the reason you are only getting the last item, for you only have one index. Let me state I am just guessing I could be wrong, but that’s where I would start debugging at.

is perfectly fine for generating one entry in an array. The problem is unlikely to be that.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.