Tagging in php and mysql
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_tag开发者_JAVA技巧s (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.
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);
}
精彩评论