Skip to content
Advertisement

MySQLi query to loop through array and update multiple rows

I have an array like:

JavaScript

And I want to loop through the array and update all of the rows where ID corresponds to the array key. Like:

JavaScript

What would be the simplest way to do this, not particularly knowing how many array keys there are, and keeping it all in one query?

Advertisement

Answer

Note: My answer is based on the PDO driver which in many aspects is better than mysqli. If you need mysqli solution please check the other answer provided by @Your Common Sense

The code below is tested on real environment and served with prepared statement preventing SQL-injection:

JavaScript

For more details about SQL injection you can read more:
https://www.owasp.org/index.php/SQL_Injection

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