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

Link to comment
Share on other sites

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] ... ]

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.