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 8767681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:52:39+00:00 2026-06-13T16:52:39+00:00

I have two tables, one is ‘tags’ that stores tid (auto increment) and name

  • 0

I have two tables, one is ‘tags’ that stores “tid” (auto increment) and “name” (name of tag). The second table “tags_data” stores the data on each tag. This table has the fields “tid” (from the first table) and a few others.

This is so people can tag content on my website. When I tag content I want to first check if that tag already exists in the first table. If it doesnt exist then we insert the tag into the DB and use the tid to insert into the second table. I have this part working so far.

The problem is when the tag already exists in the DB, I want to grab the existing tid and use it in my second query.

This is the code I have so far:

    // check if tag already exists
$tagexists = "SELECT COUNT(*) as cnt FROM tags WHERE 'name' = '$usetag' LIMIT 1";
$results = mysql_query($tagexists) or die('Invalid query: ' . mysql_error());
if ($results['cnt'] == 0) {
    // tag is not yet in DB
    $tagquery1 = "INSERT INTO tags (name) VALUES ('$usetag')";
    $result = mysql_query($tagquery1) or die('Invalid query: ' . mysql_error());
    $lastid = mysql_insert_id();
    $tagquery2 = "INSERT INTO tags_data (tid, nid, uid) VALUES ($lastid, $nid2, $uid)";
    $result = mysql_query($tagquery2) or die('Invalid query: ' . mysql_error());
} else {
    // tag is already in DB, grab the tid
    $grabtid = "SELECT tid FROM tags WHERE 'name' = '$usetag' LIMIT 1";
    $results = mysql_query($grabtid) or die('Invalid query: ' . mysql_error());
    $row = mysql_fetch_array($results);
    $lastid = $row['tid'];
    $tagquery2 = "INSERT INTO tags_data (tid, nid, uid) VALUES ($lastid, $nid2, $uid)";
    $result = mysql_query($tagquery2) or die('Invalid query: ' . mysql_error());        
}

There is something wrong with the way I am checking if a tag exists already. I was following a guide online using COUNT but it doesnt seem to be working properly.

  • 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-13T16:52:41+00:00Added an answer on June 13, 2026 at 4:52 pm

    $results['cnt'] == 0 is wrong. You are forgetting to get the result. ($results is a resource id.)

    if(mysql_result($results) == 0){
    

    Likewise, your second part of the if that gets the existing data is missing it too. You want to use $data = mysql_fetch_assoc($result); there.

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

Sidebar

Related Questions

I have two tables: one that stores blogs and the other that stores images
I have two tables one with ID and NAME table 1 ID | NAME
I have two tables that link together through an id one is submit_moderate and
I have two tables, one that has 450 rows, the other is a Sort
I have two tables one to many: Table1 ID Name 1 Abe 2 David
I have two tables one named Person , which contains columns ID and Name
Quick question that requires a long explanation.. Say I have two tables - one
Ok I have Two Tables One is business id name telephone primary_category secondary_category tertiary_category
I understand that this question refers to the trivial so I have two tables(one
I have two tables with one identical column name, but different data. I want

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.