更新了Wordpress中的多行ID

时间:2018-02-07 14:03:33

标签: php mysql wordpress

我正在尝试更新Wordpress中的多行。

<?php
$data_type = array('%d','%d','%d','%d','%d');
$convert_datatype = implode(",",$data_type);
$list_ids = array(12,1,2,3,45); 
$wpdb->query( $wpdb->prepare( "UPDATE wp_testimonials SET status = %d WHERE ID IN (".$convert_datatype.") ", 1, $list_ids ) );
?>

错误消息

  

警告:mysqli_query():第1924行的wp-includes \ wp-db.php中的空查询

2 个答案:

答案 0 :(得分:0)

尝试删除IN(“。$ convert_datatype。”)并将其替换为 IN $ convert_datatype

因此(未经测试):

$data_type = array('%d','%d','%d','%d','%d');

$convert_datatype = implode(",",$data_type);

$list_ids = array(12,1,2,3,45); 

$wpdb->query( 
   $wpdb->prepare( 
      "UPDATE wp_testimonials SET status = %d WHERE ID IN ($convert_datatype)", 1, $list_ids
   ) 
); 

尝试这是最后的手段:

 $wpdb->query( 
   $wpdb->prepare( 
      "UPDATE wp_testimonials SET status = %d WHERE ID IN ( %d, %d, %d, %d, %d )", 1, 12,1,2,3,45
   ) 
); 

答案 1 :(得分:0)

试试这个:

global $wpdb;

$list_ids = array(12,1,2,3,45);

$sql = "UPDATE wp_testimonials SET status = %d WHERE ID IN (" . implode(',', array_fill(0, count($list_ids), '%d') ) . ")";

$query = call_user_func_array(array($wpdb, 'prepare'), array_merge( array($sql), array(1), $list_ids));

$wpdb->query( $query );