0

What is wrong in my code? It doesn't get the count but it is inserting into the database.

function r_rate_recipe()
{
    global $wpdb;
    $output = array('status' => 1);
    $post_id = absint($_POST['rid']);
    $rating = round($_POST['rating'], 1);
    $user_ip = $_SERVER['REMOTE_ADDR'];

    $rating_count = $wpdb->get_var(
        "SELECT COUNT(*) FROM '" . $wpdb->prefix . "racipe_ratings'
            WHERE recipe_id='" . $post_id . "' AND user_ip='" . $user_ip . "'"
    );


    if ($rating_count > 0) {
        wp_send_json($output);
    }


    $wpdb->insert(
        $wpdb->prefix . 'racipe_ratings',
        array(
            'recipe_id' => $post_id,
            'rating' => $rating,
            'user_ip' => $user_ip
        ),
        array('%d', '%f', '%s')
    );
    $output['status'] = 2;
    wp_send_json($output);
}
-1

I think that's you must using :

$wpdb->get_results( "SELECT * FROM {$wpdb->prefix}options WHERE option_id = 1", OBJECT );

And if you give us the message of error ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.