Draw a comparison between an integer in a specific row and a count
        Posted  
        
            by 
                XCoderX
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by XCoderX
        
        
        
        Published on 2012-09-23T09:26:53Z
        Indexed on 
            2012/09/23
            9:37 UTC
        
        
        Read the original article
        Hit count: 304
        
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.";
}
© Stack Overflow or respective owner