SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Hiding from the world
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    auto increment on update?

    is there a way of increasing a number automatically when the field is updated. I want to have a voting form that just adds 1 each time a user submits.

    If i output the last number into a hidden field and add 1 it is possible that if someone has the page open for a long time other users could vote and the number stored in their form would be lower than the current number submitted by other users.

    any ideas appreciated. oh iam using php and mysql
    If i am a product of your imagination you should try harder!

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2007
    Posts
    566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use a stored procedure, which will handle the increment or create an trigger that will fire before an insert

    http://dev.mysql.com/doc/refman/5.0/en/triggers.html
    http://dev.mysql.com/doc/refman/5.0/...-routines.html

  3. #3
    SitePoint Evangelist
    Join Date
    Mar 2006
    Location
    Sweden
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about:
    UPDATE votings SET some_count = some_count + 1

  4. #4
    Django Jedi neron-fx's Avatar
    Join Date
    Sep 2007
    Location
    Bristol/Bath, UK
    Posts
    274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I like wysiwyg's suggestion

    Simple yet effective!
    Neron-Fx
    Everytime a user opens Internet Explorer, a web developer dies...
    http://www.savethedevelopers.org/

  5. #5
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    a trigger is overkill. use wysiwyg's method.
    Check out our new Industry News forum!
    Keep up-to-date with the latest SP news in the Community Crier

    I edit the SitePoint Podcast

  6. #6
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Hiding from the world
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wysiwyg View Post
    How about:
    UPDATE votings SET some_count = some_count + 1
    thanks everyone. thats what i was after as presumably it will add 1 to whatever is currently in the table rather than updating a number that has been output and had 1 added.

    thanks
    If i am a product of your imagination you should try harder!

  7. #7
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,653
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Really, you should be capturing votes then counting the votes, no incrementing a field, then totaling that. Audit trails are handy . . . .


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
  •