duansha8115 2011-01-06 00:56
浏览 26
已采纳

在php和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_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条回答 默认 最新

  • dounangqie4819 2011-01-06 01:08
    关注

    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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?