search

Home  >  Q&A  >  body text

How to operate on multiple rows when executing a SQL query

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粉311617763P粉311617763517 days ago675

reply all(1)I'll reply

  • P粉529245050

    P粉5292450502023-09-13 09:29:15

    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));

    reply
    0
  • Cancelreply