How to operate on multiple rows when executing a SQL query
P粉311617763
P粉311617763 2023-09-12 12:46:45
0
1
614

This query is valid:

$player_id = $_POST['player_id'];//array

$ids = explode(',',$player_id);
$in = implode(',', array_fill(0, count($ids), '%d'));

$wpdb->query($wpdb->prepare("DELETE FROM {$player_table} WHERE id IN ($in)", $ids));

This won’t:

$disabled = $_POST['disabled'];
$media_id = $_POST['media_id'];//array

$ids = explode(',',$media_id);
$in = implode(',', array_fill(0, count($ids), '%d'));

$wpdb->query($wpdb->prepare("UPDATE {$media_table} SET disabled = %s WHERE id IN ($in)", $disabled, $ids));

I do not understand why.

P粉311617763
P粉311617763

reply all(1)
P粉529245050

You need to scatter the IDS into separate parameters to match all %s in the query. Use ... syntax to achieve this.

$wpdb->query($wpdb->prepare("UPDATE {$media_table} SET disabled = %s WHERE id IN ($in)", $disabled, ...$ids));
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template