Jump to content

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


dominod

Recommended Posts

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

The INSERT query has an ON DUPLICATE KEY UPDATE option that will allow you to do this -

INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [iGNORE]

    [iNTO] tbl_name [(col_name,...)]

    {VALUES | VALUE} ({expr | DEFAULT},...),(...),...

    [ ON DUPLICATE KEY UPDATE

      col_name=expr

        [, col_name=expr] ... ]

 

 

Another way to do this would be using the COUNT(field) when later needing to know how many of each value there is. That way you could keep unique records of who suggested what, to prevent duplicates or "spam".

You could also do it in PHP using array_count_values.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.