0

My database schema for adding tags is the same as this question...

My question is how do I relate each tag with the current thread, so far I have done this:

    $sql = "INSERT INTO thread (title, content, author_id)
            VALUES ('$safe_title', '$safe_html_content', '$user_id')";
    $insert_thread = insert_Query($sql, $link);

    #   get the thread id:
    $thread_id = mysqli_insert_id($link);

    #   insert the tags:
    foreach ($tags as $tag)
    {
        insert_Query("INSERT INTO tags (tag) 
        VALUES ('$tag')", $link);
    }

    #   connect tags to thread:
    $sql = "INSERT INTO thread_tags (thread_id, tag_id)
            VALUES ('$thread_id', )"; ## what to do here?

I want to know how I can fill in the ItemTag table (thread_tags in my case)... I can get the id of the current thread as shown in the $thread_id var, but how do I get the id of each tag and associate it with this thread?

Thank you.

1 Answer 1

2

I'm not really sure why do you want to do it like that, but did you try this?

#   insert the tags:
foreach ($tags as $tag)
{
    insert_Query("INSERT INTO tags (tag) 
    VALUES ('$tag')", $link);
    $tag_id = mysqli_insert_id($link);
    insert_Query("INSERT INTO thread_tags (thread_id, tag_id)
        VALUES ('$thread_id', $tag_id )",$link);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.