我正在使用 wordpress 替换查询来更新/添加记录,但我遇到了错误。它成功更新记录但不在数组中的值变为 null 。这是我的代码
function ux_add_data($table,$postdata){
global $wpdb;
$tablename=$wpdb->prefix.$table;
$updated_data=$postdata['BX_data'];
if(isset($updated_data['isActive']) && $updated_data['isActive']==1)
$updated_data['isActive']=1;
else
$updated_data['isActive']=0;
$data=$wpdb->replace($tablename,$updated_data,array('%s') );
if($data)
return UX_flash('success','Data has been added successfully.');
else
return UX_flash('danger','Some errror to save your data.');
}
如果我在数据库中有列recordid,ID,name,class,created,modified
并且在数组中我只有recordid,name,modified
然后ID ,class,modified
变为 null 。
请告诉我哪里做错了。
谢谢
最佳答案
您正在使用$wpdb->replace
函数,请注意
wpdb::replace( string $table, array $data, array|string $format = null )
$data
(array) (Required) Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped). Sending a null value will cause the column to be set to NULL - the corresponding format is ignored in this case.
因此,当您设置某些列时,其他一些列将设置为 null。
查找更多 at here .
关于php - WordPress 替换查询输入 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42315951/