I am working on a plugin called Poll-lite. I want the user vote only once per poll and I do this by getting the IP address. I have the following code.
$ip = $_SERVER['REMOTE_ADDR'];
$thequery = "SELECT COUNT(*) AS total FROM `$pollresult_table_name` WHERE ip = ".$ip;
$res = $wpdb->get_results( $thequery );
if ( $res[0]->total > 0 ) {
$personip = $res[0]->total;
echo "You may vote only once in 24 hours";
} else {
$query = "INSERT INTO `$pollresult_table_name` VALUES(NULL, '$id', '".$_POST['merlic_poll_vote']."', NULL, '".$ip."', NOW())";
$wpdb->query($query);
}
What it does is that it gets the IP address and check in the database whether if that IP is already there, if it’s already there, it would return “already voted” but if it’s not yet there, then it would insert the answers of the user in the database together with its IP so that the next time the same user would vote, he can no longer vote because the IP has already been registered in the database. My problem is that it does not read my condition in my if-else. What could have gone wrong?
“.$ip;
shouldn’t that be = ‘”.$ip.”‘
also use $rows = mysql_num_rows($result);
to check the number rows returned by you’re mysql query.
more than zero means a match was found, else none.