I made a small php site with a voting system in it. I make my sql calls with PDO but can't get it working to check that a person can't vote twice a day for the same category.
Below you find my php code and sql table.
<?php
header("Expires: Tue, 01 Jan 2000 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate,max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
session_start();
$user = $_SESSION['username'];
try {
$db = new PDO('mysql:host=localhost;dbname=xxxxxxxx',"xxxxxxxxx", "xxxxxxxxxx");
}catch (PDOException $e){
echo $e->getMessage();
}
$aantalRijen = $db->query("SELECT gebruiker FROM stem WHERE UPPER(gebruiker) = '$user' AND UPPER(categorie)='dick' AND datum = CURDATE()");
$sql = "INSERT INTO stem (categorie, naam, commentaar, datum, gebruiker)
VALUES('dick','$_POST[naam]','$_POST[commentaar]',CURDATE(), '$user')";
if( $aantalRijen > 0 ) {
echo nl2br("U heeft al een stem uitgebracht \n");
echo "<script>setTimeout(\"location.href = 'index.php';\",1500); </script>";
}
else {
$results = $db->exec($sql);
echo "Stem succesvol uitgebracht!";
echo "<script>setTimeout(\"location.href = 'index.php';\",1500); </script>";
}
$results = NULL;
$aantalRijen = NULL;
$db = NULL;
?>
Screenshot of the database: http://prntscr.com/bjgx6w