$wpdb->update() always need a second try
I will use a custom endpoint of REST APi to update fields in a custom table. Therefore, i wrote this function:
function update_table_single($request = null){
global $wpdb;
$data_key = trim($request['data_key']);
$data_value = trim($request['data_value']);
$where_key = trim($request['where_key']);
$where_value = trim($request['where_value']);
$table = trim($request['table']);
$data = [ $data_key = $data_value ];
$where = [ $where_key = $where_value ];
$wpdbupdate = $wpdb-update($wpdb-prefix.$table, $data, $where);
// $wpdbupdate = $wpdb-query(
// $wpdb-prepare( UPDATE $wpdb-prefix.$table SET $data_key = %s WHERE $where_key = %d, $data_value, $where_value )
// );
$wpdb-show_errors = TRUE;
$wpdb-suppress_errors = FALSE;
if($wpdbupdate === 0){
$response['error'] = $wpdb-last_error;
$response['query'] = $wpdb-last_query;
$response['status'] = 200;
}else{
$response['error'] = $wpdb-last_error;
$response['query'] = $wpdb-last_query;
$response['status'] = 400;
}
return new WP_REST_Response($response, 123);
}
But the strange thing is, that the first try to update the field with a new value always returns false (status:400
).
The second try (just send POST request again) will update the field as expected.
In both cases, the queries are exactly the same:
UPDATE izq5I_my_customer SET user_color = 'red' WHERE user_id = 2169
I also tried the prepare()
function, but this gives the same results.
What else can i try to find the reason for this strange behavior?
Topic wp-update-post wpdb plugin-development Wordpress
Category Web