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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With