Blocking user submissions for 24 hours, and showing how many hours left on the block
In the code below, datesubmitted
is a timestamp. If a given $uid
has submitted within 24 hours, I want them to not be able to submit, and see a message that shows how many hours until they can submit again.
The code below seems to work for at le开发者_如何学编程ast the first few hours (it shows 24 during the first hour, then 23 during the 2nd hour). But when I checked today and expected to see 4-6 hours left on the message, the message wasn't there at all.
Is there something wrong with my code?
$queryuidcount = "select loginid from submission where TO_DAYS(datesubmitted) = TO_DAYS(NOW()) AND loginid = '$uid'";
$uidresult = mysql_query($queryuidcount);
if (mysql_num_rows($uidresult) >= 1) {
$queryuidhours = "select loginid, 24 - HOUR(TIMEDIFF(NOW(), datesubmitted)) as hours from submission where loginid = '$uid' ORDER BY hours DESC";
$hourresult = mysql_query($queryuidhours);
$hourcount = 1;
while($row = mysql_fetch_array($hourresult)) {
echo '<div class="submittitle">You will not be able to submit again for '.$row["hours"].' hours.</div>';
break;
$hourcount++;
}
mysql_free_result($hourresult);
} else {
What's the first SQL query for? Once midnight has passed, it will return nothing, even if a submission happened just minutes before midnight.
(And what's up with the while
loop that is broken out of unconditionally in the middle of the first iteration?)
Also also: your queries look unnecessarily demanding on the database. It would probably be much more efficient if you start by computing a cut-off timestamp as "24 hours ago" and then simply select rows that are newer than that. Once you get a row back (if you do), you can compute how long time is left on the PHP side instead. (Or even just retrieve the most recent submission time and then do the entire embargo calculation outside the database).
精彩评论