Part of a voting script I’m working on: I’m checking a custom table in my WP database to see if a user already voted for a post by checking the IP address and the post id.
If the user’s IP already exists for the post they voted, I want to echo “Already voted!” otherwise add the IP. This is what I came up with..
global $wpdb;
$voter_ip = $_SERVER['REMOTE_ADDR'];
$post_id = $_POST['post_id'];
if (!($wpdb->get_row("SELECT voter_ip FROM wp_voter_ips WHERE post_id = $post_id AND voter_ip = $voter_ip") ) ) { //if IP address for matching post id not found in wp_voter_ips table
$wpdb->insert( $wpdb->prefix . 'voter_ips', array( 'post_id' => $post_id, 'voter_ip' => $voter_ip ) ); //add IP and post_id
echo "voted!";
}
else //if IP already exists
{
echo "Already voted!";
}
I triple checked to make sure the variables and column names are correct. Regardless of the if statement, it inserts the voters IP even when it already exists.
On your
if()
, it should be like this:Notice the quotation mark added on
$voter_ip
.