Skip to content
Advertisement

Updating value in array updates all values with the last inserted value PHP

I have two arrays, one with quantities called aantal and one with products called producten.

Producten:

JavaScript

Aantal:

JavaScript

I want to update each quantity according to the $_GET values from the url with the following code:

JavaScript

This is the form with the html:

JavaScript

Why if I click the update button, it updates the last inserted values for all products? For example if I set the last quantity input at 25 and the one before it at 10, all products have a quantity for 25.

How can I fix that?

Advertisement

Answer

Your inner foreach loop is the problem. You’re going through all the quantities instead of picking the related one. So when the loop ends you’re always left with the last one being added to your session.

I can’t test right now but I think you should change

JavaScript

to

JavaScript

That way, you select the matching key for both arrays.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement