Making a function that determines whether or not a user has 10+ entries per calendar day
I am using a table in MySQL called "submissions" that contains fields called "loginid" and "datesubmitted." "Loginid" is an integer and "datedubmi开发者_运维技巧tted" is a timestamp.
I would like to make a function that will determine whether or not a given "$uid" equals a "loginid" that has 11 or more rows in "submissions" with a "datesubmitted" of today's calendar date.
I'm not sure how to make this, but this is what I have so far:
function uidlimit($uidinput){
$queryuid = "select uid from submission where datesubmitted > '$todaysDate 00:00' and date < '$todaysDate 23:59' group by uid having count() > 11";
$uidresult = mysql_query($queryuid);
Any advice on how I can make such a function?
Thanks in advance,
John
EDIT: Thanks to Pekka, I ended up using this, which works:
$queryuidcount = "select loginid from submission where TO_DAYS(datesubmitted) = TO_DAYS(NOW()) AND loginid = '$uid'"; // Remove line breaks
$uidresult = mysql_query($queryuidcount);
if (mysql_num_rows($uidresult) >= 11)
{
session_write_close();
header("Location:http://www.domain.com/sample/index.php");
exit;
}
Why not simply get the number of submissions for the given user, and count them in PHP?
function uidlimit($uidinput){
$uidinput = mysql_real_escape_string($uidinput); // Don't forget to sanitize!
$queryuid = "select count(uid) as cnt from submission
where TO_DAYS(datesubmitted) = TO_DAYS(NOW())
AND uid = '$uidinput'"; // Remove line breaks
$uidresult = mysql_query($queryuid);
$record = mysql_fetch_row($uidresult);
if ($record->cnt >= 11) echo "11 or more!";
else echo "less than 11!";
精彩评论