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

  • SEARCH
  • Home
  • 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 8514405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:51:33+00:00 2026-06-11T04:51:33+00:00

I had table like this: post: +—————-+———–+ | article_id | tags | +—————-+———–+ |

  • 0

I had table like this:

post:
+----------------+-----------+
| article_id     | tags      | 
+----------------+-----------+
| 1              |  x1,x2,x3| 
| 2              |  x1,x4,x5 | 
+----------------+-----------+

what i am trying to do is to normalize it, so i create 2 more tables:

tags:
+----------------+-----------+
| tag_id         | tag_name  | 
+----------------+-----------+
| 1              |    x1     | 
| 2              |    x2     | 
| 3              |    x3     |
+----------------+-----------+

post_tag:
+----------------+-----------+
| id_post         | id_tag  | 
+----------------+-----------+
| 1              |    1     | 
| 1              |    2     | 
| 2              |    1     |
+----------------+-----------+

I have exported all tags from post table to tags table and now each tag has his own id in tags table, but I am so confused when i am thinking how to do it with post_tag table?

edit: questin is how to populate post_tag table, how the query would look like?

tried like this

$sql=mysql_query("SELECT * FROM post"); 
    while($row=mysql_fetch_array($sql)) {
        $article_id=$row['article_id'];
  $all_tags =$row['tags'];      
  $tags= explode(",",$all_tags);                                
    foreach($all_tags as $tag) {
     $sql2=mysql_query("SELECT * FROM tags WHERE tag_name = '$tag'");
      while($row=mysql_fetch_array($sql2)) {                               $tag_id=$row['tag_id'];                                  $q = "INSERT INTO post_tag (id_post, id_tag) VALUE ('$article_id', $tag_id')";
        $r = mysql_query($q);
      }
    }
}

but it doesn’t write complete post_tag table

  • 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-11T04:51:35+00:00Added an answer on June 11, 2026 at 4:51 am

    Maybe this one will work?

    $sql= mysql_query("SELECT * FROM post"); 
    while($row=mysql_fetch_array($sql)) {
        $article_id=$row['article_id'];
        $all_tags = $row['tags'];      
        $tags = explode(",",$all_tags);              
        foreach($all_tags as $tag_title) {
            $tag_id = null;
            $tag=mysql_fetch_assoc(mysql_query('SELECT * FROM tags WHERE tag_name = "'.$tag_title.'"'));
            if ($tag) {
                $tag_id = $tag['tag_id']; 
            } else {
                mysql_query('INSERT INTO tags SET tag_name = "'.$tag_title.'"');
                $tag_id = mysql_insert_id();
            } 
            $q = "INSERT INTO post_tag (id_post, id_tag) VALUE ('$article_id', $tag_id')";
            $r = mysql_query($q);
        }
    }
    

    After running the loop, you will need to drop the old column in post table

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

Sidebar

Related Questions

I had a table like this id | name ------------------ 1 | SAM1 2
I've got a form that looks like this: <form method=post> {% csrf_token %} <table>
If I had a CarsDataStore representing a table something like: Cars -------------- Ford |
I am trying to pull out data from the table I had from the
Mostly my entries in seeds.rb are simple, like this: User.create!( name: Peter admin: false;
I am trying to implement an autocomplete in ASP.NET MVC 3, following this post
I have got two tables: tbl_sms tbl_bids The above two table holds something like
I have a table (this cannot be changed) like the following: POST_ID | PARENT_ID
Hi I had parent table which has ID and some other columns and a
I previously had a table that is created using hibernate entities. I am using

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.