Advanced Search

Results 1 to 4 of 4

Thread: Increment a Value Stored in the DB

  1. #1
    Join Date
    Nov 2007
    Location
    USA
    Posts
    170
    Thanks
    8
    Thanked 22 Times in 22 Posts

    Default Increment a Value Stored in the DB

    Hello,

    I have some fields in my DB that when a certain thing happens I just want to increment the value in it by a number.

    Say the field contains this: 3
    After the action I want to add 1 to it so that it will now be this: 4

    I have set the field to an INT but I am not sure what kind of SQL statement I would need to write.

    Would I use UPDATE to change it or is there some kind of command where I can just INCREMENT it or something?

    Thanks for any help!
    What is obvious to you might not be to another.


    My Site

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    UPDATE your_table SET your_field = your_field + 1;
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Nov 2007
    Location
    USA
    Posts
    170
    Thanks
    8
    Thanked 22 Times in 22 Posts

    Default

    This keeps giving me an 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 ''Poll Number 1' SET 'three = three + 1'' at line 1
    What is obvious to you might not be to another.


    My Site

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    That's because ‘Poll Number 1’ is not a valid SQL identifier. If you want to use it then you need to enclose it in backticks (`), but really I suggest you just rename it.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •