I have a php code that votes I want to prevent users from voting twice. I want to check if user if the user has voted, and prevent them from voting again. This is my code below:
<?php
include('conn.php');
$expire = 24*3600*30; //after one day
setcookie('SnugglesVotingSystem'.$_POST['id'], 'voted', time() + $expire, '/'); // Place a cookie
if($_POST)
{
try{
$query = "UPDATE tbl_images SET up =up".$_POST['value']." WHERE id=".$_POST['id'].""; // Update number with +1 or -1
// Prepare statement
$stmt = $DB->prepare($query);
// execute the query
$stmt->execute();
// echo a message to say the UPDATE succeeded
echo $stmt->rowCount();
}
catch(PDOException $e)
{
echo $query . "<br>" . $e->getMessage();
}
$value = "SELECT up FROM tbl_images WHERE id=".$_POST['id'].""; // Get the new number value
$stmt = $DB->prepare($value);
$stmt->execute();
$images = $stmt->fetchAll();
if (is_array($value) && isset($_POST['up'])) {
echo $value['up'];
}
//echo $value['up']; // Send back the new number value
}
?>