SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Taunton, UK
    Posts
    787
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Insert counter if doesn't exist otherwise update it.

    I want to limit the uploads on a site to 3 uploads per user.

    I have therefore created a separate table to store a count of the uploads per email address as follows


    Code:
    CREATE TABLE IF NOT EXISTS `uploadsperuser` (
      `strEmail` varchar(255) NOT NULL,
      `intCount` int(3) NOT NULL,
      PRIMARY KEY (`strEmail`,`intCount`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
    Before inserting a record in the uploads table I want to check the count and insert or increment depending on which is necessary.

    Is the following close to what I need?

    Code:
    $biEmail = "tester@test.com";
    
    $sql = "INSERT INTO uploadsperuser (strEmail, intCount) VALUES ($biEmail, 1) ON DUPLICATE KEY UPDATE intCount = intCount + 1";
    Thanks inadvance
    Mediakitchen Limited
    App Development | Website Design & Development | Flash Game Development
    Somerset, UK
    http://www.mediakitchen.co.uk

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Your primary key has to be only strEmail, otherwise once the counter has been updated to 2, the next time you'll insert a new row with counter value 1 for the same email.

  3. #3
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Taunton, UK
    Posts
    787
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Guido for spotting this error. Much appreciated. I haven't done any database work for quite some time so very rusty.

  4. #4
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Taunton, UK
    Posts
    787
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am getting the following error


    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' '1') ON DUPLICATE KEY UPDATE intCount = intCount + 1' at line 1

    Here is the code - any ideas what the syntax error is?

    Code:
    $biEmail = "tester@test.com";
    
    $sql = "INSERT INTO uploadsperuser (strEmail, intCount) VALUES ($biEmail, 1) ON DUPLICATE KEY UPDATE intCount = intCount + 1";
    Mediakitchen Limited
    App Development | Website Design & Development | Flash Game Development
    Somerset, UK
    http://www.mediakitchen.co.uk

  5. #5
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Taunton, UK
    Posts
    787
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No worries, I worked out the problem.

    Should have been

    Code:
    $sql = "INSERT INTO uploadsperuser (strEmail, intCount) VALUES ('$biEmail', '1') ON DUPLICATE KEY UPDATE intCount = intCount + 1";
    Mediakitchen Limited
    App Development | Website Design & Development | Flash Game Development
    Somerset, UK
    http://www.mediakitchen.co.uk


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •