I am trying to make a rating system to my posts. First I have created a custom table that will contain post_id, post_title_, user ip, user machine, post rate, username, user id, timestamp.
Until now all is fine and I can insert data without any issue into this table. Next I am trying to create a function to verify if the user has already voted, to not allow user to rate more than one time per post, so created this function
function alreadyvoted($post_id) {
// variables
global $wpdb;
$table_name = $wpdb - > prefix.
"ratings_fansub";
$ip = $_SERVER['REMOTE_ADDR'];
$rate_userid = get_current_user_id();
$rate_user_machine = @gethostbyaddr($ip);
// getting data from databaase by current post_id
$results = $wpdb - > get_results('SELECT * FROM $table_name WHERE ratings_fansub_postid = $post_id ');
$NumRows = count($results);
$RandNum = rand(0, $NumRows);
$i = 0;
while ($i < $RandNum):
if ($rate_userid != 0) {
if ($rate_userid == $results[$RandNum] - > ratings_fansub_username)
return 1;
} else {
if ($rate_user_machine == $results[$RandNum] - > ratings_fansub_host or $ip == $results[$RandNum] - > ratings_fansub_ip)
return 1;
}
$i++;
endwhile;
}
But this function does not work, I believe I have made a mistake somewhere in the sql line but I can’t figure out what it is.
Please check your table name in database. Table name like “wp_ratings_fansub” or not.
OR you can also print table name ‘echo $wpdb – > prefix.”ratings_fansub”;exit();’
and check database table name is match or not.
i have fix it,