PHP / mySQL query causing errors with memberpress and wordpress

116 Views Asked by At

So... we have memberpress installed and I have come in as new developer.

there is this code below, written by previous developer....


    if($user_meta['mepr_is_training_active'][0] != ''  && $user_meta['mepr_is_training_active'][0] == 'on'){
        $have_training = 'yes';
    }
    else{
        $mpca_id = get_user_meta($user_id, 'mpca_corporate_account_id', true);
        global $wpdb;
        $sql = $wpdb->prepare( "SELECT user_id FROM wp_mepr_corporate_accounts WHERE id = ".$mpca_id);
        $results = $wpdb->get_results($sql);
        foreach($results as $result){
            if(!empty($result->user_id) || $result->user_id != '' || $result->user_id != ' '){
                $user_meta_parent = get_user_meta(trim($result->user_id));
                if($user_meta_parent['mepr_is_training_active'][0] != ''  && $user_meta_parent['mepr_is_training_active'][0] == 'on'){
                    $have_training = 'yes';
                }
            }   
        }   
    }

Our error logs show the following:

WordPress database error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 for query SELECT user_id FROM wp_mepr_corporate_accounts WHERE id =  

I do understand most of this, is it the use of quotation marks not being correct?

help much appreciated.

thanks

have attempted to replace quotation marks, cant see the problem.

1

There are 1 best solutions below

4
On BEST ANSWER

Try replacing $wpdb->prepare statement with this one.

$sql = $wpdb->prepare( "SELECT user_id FROM wp_mepr_corporate_accounts WHERE id = %d", $mpca_id);