Frage

I'm writing a PHP competition script for a members site that needs to restrict entries to one per day per member. So far I have the following MySQL code:

SELECT ce_id 
  FROM competition_entries 
 WHERE ce_c_id = '$c_id' 
       AND ce_sub_id = '$user_id' 
       AND cte_date >= SYSDATE() - INTERVAL 1 DAY
  • ce_c_id is the competition ID,
  • ce_sub_id is the member ID, and
  • cte_date is a MYSQL datetime stamp for the entry.

It's hard for me to test from where I am now & I need to find a solution, so I'm hoping someone can tell me whether this is restricting to once-per-day or once-per-24hrs - and point me in the right direction if it's the latter.

TIA :)

War es hilfreich?

Lösung

Create a primary key composed of the user_id, competition_id and a date type column.

To check if the user has already placed an entry:

select count(*)
from competition_entries
where ce_c_id = '$c_id' 
    AND ce_sub_id = '$user_id' 
    AND cte_date = current_date()

Andere Tipps

I'm hoping someone can tell me whether this is restricting to once-per-day or once-per-24hrs

Looks like it's 24 hours:

mysql> select sysdate(), sysdate() + interval 1 day;
+---------------------+----------------------------+
| sysdate()           | sysdate() + interval 1 day |
+---------------------+----------------------------+
| 2011-03-21 15:50:56 | 2011-03-22 15:50:56        | 
+---------------------+----------------------------+
1 row in set (0.01 sec)

If you need "tomorrow", as in, tonight at one minute past 23:59, consider chomping down things to plain old DATE's resolution:

mysql> select DATE(sysdate()), DATE(sysdate()) + interval 1 day;
+-----------------+----------------------------------+
| DATE(sysdate()) | DATE(sysdate()) + interval 1 day |
+-----------------+----------------------------------+
| 2011-03-21      | 2011-03-22                       | 
+-----------------+----------------------------------+
1 row in set (0.00 sec)

By just considering dates instead of times, your cutoff will effectively expire at midnight. Watch out, though -- you'll then be at the mercy of the time on your MySQL server, which might differ from the time on your application server if they are on different machines.

Can't be sure without testing, but I'd hazard a guess that it's once every 24h.

Try the following instead:

SELECT ce_id FROM competition_entries WHERE ce_c_id = '$c_id' AND ce_sub_id = '$user_id' AND DATE(cte_date) == DATE(SYSDATE())

Here you are clearly comparing two dates, one from your field and the other from the current date for equality.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top