I’m working on a project where I need to update a database with the values of an array. The starting value must be at 1, instead of 0, so that the right database entries are selected with the update query.
When I print the array (seen below) the results come back starting with a value of 1 but the database isn’t updated correctly. The database is still being updated with the 0 value for the array.
foreach($rowPrice as $priceID => $price) {
$rowPrice = array_combine(range(1, count($rowPrice)), array_values($rowPrice));
mysql_query("UPDATE ---- SET price='$price' WHERE id='$priceID' AND store_id='$store' LIMIT 1") or die (mysql_error());
}
This is part of the array when it is printed. This part is working but not translating through the update query.

This is how the entries in the database are updated.

As you can see the query is only inserting the array values starting at 2.
What am I doing wrong? Is there a better way to do this?
Reindex
$rowPricebefore you start iterating over it.However this method seems fragile as it makes assumptions about the association between the values in your array and the IDs in the database. It’d be better have the original IDs in the array so you can refer to them during the update.