Question

Une partie d'un script de vote je travaille. Je vérifie une table personnalisée dans ma base de données de WP pour voir si elle a déjà voté un utilisateur pour un poste en vérifiant l'adresse IP et l'ID poste

Si IP de l'utilisateur existe déjà pour le poste ils ont voté, je veux faire écho « déjà voté! » sinon ajouter l'adresse IP. Voilà ce que je suis venu avec ..

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!";
}

J'ai vérifié trois pour vous assurer que les variables et les noms de colonnes sont corrects. Quelle que soit l'instruction if, il insère les électeurs la PI même quand il existe déjà.

Était-ce utile?

La solution

Sur votre if(), il devrait être comme ceci:

if (!($wpdb->get_row("SELECT voter_ip FROM " . $wpdb->prefix . "voter_ips WHERE post_id = $post_id AND voter_ip = '$voter_ip'")

Notez la guillemet ajoutée sur $voter_ip.

Licencié sous: CC-BY-SA avec attribution
Non affilié à wordpress.stackexchange
scroll top