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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:58:38+00:00 2026-06-05T04:58:38+00:00

Previously I asked how to run a fetch query on a many to many

  • 0

Previously I asked how to run a fetch query on a many to many relation ship database, as I am progressing with the learning path of SQL + PHP and I ran into the following rather complex situation:

I’ve got a database with the following tables:

Songs       Link        Tags
=======     =====       =========
Sid          Sid        Tid
Songname     Tid        Tagname

How do you create (in PHP with a HTML form) a set of queries that check for the following:

A new song is enterred with some tags. You add the Song to the Songs table (fairly easy) and the Sid auto increments.
The tags need also to be stored, but not double. How do you create a way to check if the tags entered are not in the tags table already? if not add them and if they are already in there skip them.

On top of that the link table must also be (automatically and correctly) filled with the info. The new Sid must be linked with the new added / old tags out of the Tags table.

I don’t even know if this complex series of queries is possible, but doing this by hand grows rapidly out of control, so I thought an automated way is the way to go.

So far I’ve got the following part of code to test with (the code obviously doesn’t do much at this point, as it’s only to illustrate what I’m up to):

<?php

if(isset($_POST['song']) && isset($_POST['tags'])){
$song = $_POST['song'];
$temptag = $_POST['tags'];

$tags = explode(",", $temptag);

$connection = mysql_connect("servername","db","password");
if (!$connection)
  {
  die('Could not connect: ' . mysql_error());
  }

  $query = "INSERT INTO Songs VALUES ('','$song')"; #add song
  mysql_query($query);

  # now for the tags...
  mysql_close($connection);
  echo "Song added";
}
else{
?>

<form action="add.php" method="post">
<input type="text" name="song" /> <br/>
<input type="text" name="tags" value="tag1,tag2,tag3" /> <br/>
<input type="submit" value="add song" />
</form>
<?php
}
?>

Thanks in advance.

  • 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-05T04:58:40+00:00Added an answer on June 5, 2026 at 4:58 am

    Add this after you insert the song

    $song_id = mysql_insert_id();
    $tags = explode("," , $_POST['tags']);
    foreach($tags as $tag) {
        // check if the tag already exists
        $res = mysql_query("SELECT * FROM `Tags` WHERE `Tagname` = '".$tag."' LIMIT 1");
        if(mysql_num_rows($res) == 0) {
            // if it doesn't exist, we add it
            mysql_query("INSERT INTO `Tags` VALUES ('' '".$tag."')");
            // get the last id inserted
            $tag_id = mysql_insert_id();
        } else {
             // if found, get it's id
             $tag = mysql_fetch_assoc($res);
             $tag_id = $tag[0]['id'];
        }
        // linkage
        mysql_query("INSERT INTO `Link` VALUES ('".$song_id."', '".$tag_id."')");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This problem is similar to my previously asked question. When I query data using
I previously asked a question regarding pulling specific items out of a database if
I previously asked a question about fetching the last 100 mentions for a person
I previously asked, How to determine the target of a symbolic link or Reparse
I previously asked a question about chaining conditions in Linq To Entities. Now I
I previously asked why \n in a string was not returning a line break.
Hi guys i previously asked a question and got a good solution. here is
I've previously asked a question related to generating a random unsigned char and pretty
This is an extension of a question I previously asked here . Long story
I am facing an issue with a button onclick. I checked with previously asked

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.