Snaggle Posted July 3, 2010 Share Posted July 3, 2010 I have a user system that lets users add points to their accounts. I want to restrict this so that they can only do this once every 24 hours. I've had a mess around in SQL with the time command but I can't figure out how to do this. Anyone know how I can limit their account to only 1 vote per 24 hours? Quote Link to comment https://forums.phpfreaks.com/topic/206588-daily-limits/ Share on other sites More sharing options...
sw0o0sh Posted July 3, 2010 Share Posted July 3, 2010 Here is how I would approach it. Could be the best way, might not be. See what other people say. But anyway, Create a last_vote column INT(10) in the table. When they vote, mysql_query("UPDATE table SET last_vote = '" . (time()) . "' WHERE user = 'whatever';"); Now, that sets the last time they voted. Handle the actual code with something like this. <?php $last_vote_time = mysql_fetch_assoc(mysql_query("SELECT last_vote FROM table WHERE username='whatever'")); $restrict_24hours = $last_vote_time['last_vote'] + 60 * 60 * 24; if(isset($last_vote_time['last_vote']) && $restrict_24hours > time()){ echo "Please wait 24 hours to use this function again"; } else { echo "You can vote"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/206588-daily-limits/#findComment-1080527 Share on other sites More sharing options...
Snaggle Posted July 3, 2010 Author Share Posted July 3, 2010 I tried that query but it doesn't seem to be writting into my SQL. The field just stays as 0. Quote Link to comment https://forums.phpfreaks.com/topic/206588-daily-limits/#findComment-1080537 Share on other sites More sharing options...
sw0o0sh Posted July 3, 2010 Share Posted July 3, 2010 Make sure the last_vote is an INT(10). This is so it can receive UNIX timestamps. You basically generate a timestamp, by requesting time(). So I'll break it down a bit more: $current_time = time(); $update_time = mysql_query("UPDATE table SET last_vote = '" . $current_time . "' WHERE username = 'whatever'"); As for other basics, make sure you have at least established a db connection and that querying in general can work. There should be nothing wrong with updating the time, I've tested myself Quote Link to comment https://forums.phpfreaks.com/topic/206588-daily-limits/#findComment-1080540 Share on other sites More sharing options...
Philip Posted July 3, 2010 Share Posted July 3, 2010 Depends how you have your current 'point' system setup right now. Can you post more info (db schema)? mysql_query("UPDATE table SET last_vote = '" . (time()) . "' WHERE user = 'whatever';"); I'd use mysql's time/date and update with NOW() Quote Link to comment https://forums.phpfreaks.com/topic/206588-daily-limits/#findComment-1080541 Share on other sites More sharing options...
Snaggle Posted July 3, 2010 Author Share Posted July 3, 2010 Thanks I got it working in the end Quote Link to comment https://forums.phpfreaks.com/topic/206588-daily-limits/#findComment-1080550 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.