Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

updating multiple rows in one sql query

Tags:

sql

php

mysql

I have to update an arbitrary number of rows for a list of customers, when reminders are sent out to them. I can achieve this through a foreach loop, but I am sure there must be a way of doing this with on query.

This is what currently works

foreach($customer_id_arr as $c) mysqli_query($con,"UPDATE crm_customers SET customer_reminded=1 WHERE customer_id=$c");

How can I do that in one query?

like image 669
Source Avatar asked Feb 07 '26 01:02

Source


1 Answers

You can leverage implode() for this:

mysqli_query($con,"UPDATE crm_customers SET customer_reminded=1 WHERE customer_id IN (" . implode(",",$customer_id_arr) . ")";

It's worth mentioning that you're likely wide open to SQL injection without using prepared statements.

like image 134
esqew Avatar answered Feb 09 '26 16:02

esqew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!