Skip to content
Advertisement

WordPress: How to update data in database using PHP with MySQL?

I am using core PHP to make WordPress custom page template for my website, but when I am updating user id, it was not updating the user id.

Suppose I have a user in database and when the user pay amount using PayPal, make that user id 1. By default its id is 0.

$wpdb->query( $wpdb->prepare( "UPDATE user_register SET pay_status = '$pay_status' WHERE wp_user_id = $current_user" ) );

I want to make that user status to 1 when paid.

Advertisement

Answer

Your code should work. But without knowing the full source code we don’t know why it doesn’t work.

Although you can do update $wpdb->query, $wpdb does have a better way to update a row. Use $wpdb->update instead.

if($user_is_paying){
    global $wpdb;
    $table = 'user_register';
    $data = array('pay_status'=>$pay_status);
    $where = array('wp_user_id'=>$current_user);
    $wpdb->update( $table, $data, $where);
}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement