Draw a comparison between an integer in a specific row and a count
- by XCoderX
This is a follow-up question to this one: Check for specific integer in a row WHERE user = $name
I want a user to be able to comment on my site for exactly five times a day. After this five times, the user has to wait 24 hours.
In order to accomplish that I raise a counter in my MYSQL database, right next to the user.
So where the name of the user is, there is where the counter gets raised. When it reaches 5 it should stop counting and reset after 24 hours. In order to check the time I use a timestamp. I check if the timestamp is older than 24 hours. If that is the case, the counter gets reseted (-5) and the user can comment again. In order to do that, I use the following code, however it never stops at five, my guess is that my comparison is wrong somehow:
$counter = "SELECT FROM table VALUES CommentCounterReset WHERE Name = '$name'";
if(!isset($_SESSION['ts'])); {
$_SESSION['ts'] = time();
}
if ($counter >= 5) {
if (time() - $_SESSION['ts'] <= 60*60*24){
echo "You already wrote five comments.";
}
else {
$sql = "UPDATE table SET CommentCounterReset = CommentCounterReset-5 WHERE Name = '$name'";
}
}
else {
$sql = "UPDATE table SET CommentCounterReset = CommentCounterReset+1 WHERE Name = '$name'";
echo "Your comment has been added.";
}