Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8862831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:49:24+00:00 2026-06-14T15:49:24+00:00

We have a table named tags which has 3 cols: id, value, count which

  • 0

We have a table named “tags” which has 3 cols: id, value, count which the value is Unique key.

our members are supposed to enter tags for their favorite musics, we’re gonna save each member tag in “members.favorites” table, for the auto suggestion we query the “tags” table.

Now a member favorites are: “song1,song2,song3”, we put that value exactly like that in “members.favorites”, now for putting the in “tags” we’re gonna explode that by ‘,’ and insert it into “tags”, so “tags” gets 3 rows as “song1”, “song2”, “song3” with “count=1”, now another member comes and enter “song3,song4,song5”, for entering this into “tags”, “song4” and “song5” will be inserted as a new row since they don’t exist, but the “song3” already exists and I need to do “count+1” for “song3”.

My code is:

$tags_array =  explode(',', $tags);
foreach($tags_array as $key => $val){
    $check_if_already_exists = mysql_query("SELECT value FROM tags WHERE value = '$val'");
    if(mysql_num_rows($check_if_already_exists)){
        // This already exists, so count++
        mysql_query("UPDATE tags SET count = count+1 WHERE value = '$val'");    
    }else{
        // It's a new tag, insert
        mysql_query("INSERT INTO tags (value, count) VALUES ('$val', '1')");
    }
}

But this seems wired somehow! each user may have 20 favorite song and the above code seems not to have the best performance, anybody got any ideas for improving this?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:49:25+00:00Added an answer on June 14, 2026 at 3:49 pm

    You can use INSERT … ON DUPLICATE KEY UPDATE syntax

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example, I have a table, and there is a column named 'Tags'. I
I have a table named tbl_Subjects_Taken which lists all the subjects taken by the
Say I have a Companies table which has a many-many relationship with an Employees
I have 2 tables: comments & blog blog has the following fields: id(Unique key),title,
Hi, I have a table of 16 td tags.Every td has mouser over and
I have a php page which has multiple form tags. each form is dealing
I have a Tags table whose schema consists of only ID and Name (unique).
I have a file import.php in which html data is written under table tags.
Lets say I have table named Place with columns: placeId int not null auto_increment,
I have a table named datas and I'm executing a query like this: SELECT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.