Make a row increase every time the same value is added to the table?

Discussion in 'PHP' started by misterdh, Jul 27, 2010.

  1. #1
    Hi

    Is it possible to make a row(with number) increase every time the same value is added to the table?

    Example:

    I enter "test" into a table and it gets the following data:

    id=1 name=test count=1

    And the next time I enter "test" into the table it becomes

    id=1 name=test count=2

    instead of

    id=1 name=test count=1
    id=2 name=test count=1

    I want to create a form where users can suggest stuff and I want to see what is the most popular ect.

    Thanks in advance :)
     
    misterdh, Jul 27, 2010 IP
  2. ze0xify

    ze0xify Peon

    Messages:
    13
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    In MySQL:

    Make the query similar to:
    INSERT INTO `table` (`id`, `name`, `count`) VALUES ($id, $name, 1) ON DUPLICATE KEY UPDATE `count`=`count`+1;
    Code (markup):
    So if the name inserted is new, then you start out with a single count. When multiple instances are created, it uses the duplicate key portion of the code and increments the count value instead.
     
    ze0xify, Jul 27, 2010 IP
    Deacalion likes this.
  3. upl8t

    upl8t Peon

    Messages:
    80
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    This is a great solution. I used to query for a count and then either insert or update. This handles it all in one clean insert statement.
     
    upl8t, Jul 27, 2010 IP
  4. sandy_joy

    sandy_joy Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks zeOxify, for sharing that useful info to all.
     
    sandy_joy, Jul 27, 2010 IP
  5. misterdh

    misterdh Peon

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks ze0xify! ;)
     
    misterdh, Aug 1, 2010 IP