Search code examples
phpmysqldaterestrict

Restrict competition entry to once per day


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 :)


Solution

  • 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()