mercredi 15 juin 2016

Check if IP is in database, if not add it and increment a value

I'm building a site that lets users anonymously post messages to a board and rate other messages. Because it's anonymous and doesn't use any kind of user id, when someone clicks to vote up or down on a post, I need to store that post_id and their IP address so they can't vote again.

Here's the system I have in place, it does not work and I don't know why:

mysqli_autocommit($connection, TRUE);
mysqli_query($connection, "UPDATE $tablename SET downvotes = (downvotes + 1) WHERE id = $post_id AND NOT EXISTS (SELECT 1 FROM $table2name WHERE ip = $ip_usr AND post_id = $post_id");

if(mysqli_affected_rows($connection) > 0) {
    $insert = mysqli_query($connection, "INSERT INTO $table2name VALUES($post_id, $ip_usr)");
    $insert ?
        mysqli_commit($connection):
        mysqli_rollback($connection);
} else {
    mysqli_rollback($connection);
}

I received a tip that instead of using mysqli_autocommit() I should use mysqli_begin_transaction(), however I am using PHP version 5.2 and that function doesn't exist yet, and I have no way of upgrading my PHP version.

The gist of what I'm trying to do is when someone tries to vote on a post, if they haven't voted yet, their vote is made and their IP and that post's id are added into a table. If they have already voted, nothing happens.

Can anyone please help me? Thanks!

Aucun commentaire:

Enregistrer un commentaire