【问题标题】:How to store specific tags in database如何将特定标签存储在数据库中
【发布时间】:2015-07-24 18:49:22
【问题描述】:

我阅读了很多关于标签的帖子,但似乎没有一个能做我正在做的事情。 我有这个帖子上传器,您有 6 个标签,您可以通过单击每个类别旁边的复选框来选择任意数量的标签,并将值发布到 php。

没关系,我只是不知道如何将标签存储在数据库中。 如果有人上传与 4 个类别有关的内容,我将有 4 个标签变量,我将它们插入某处。存储标签的最佳方法是什么,以便我以后可以按标签将帖子分类到不同的页面?

编辑:

类别复选框的 HTML 是这样的:

<ul class="selectionList">
    <li>
        <div class="markerDiv"  id="maker_school">
            <input class="marker_ckeckbox" name="markerType" value="Naruto" type="checkbox"  /> 
            <label class="marker_label">Naruto</label> 
        </div>
    </li>
    <li>
        <div class="markerDiv"  id="maker_school">
            <input class="marker_ckeckbox" name="markerType" value="OnePiece" type="checkbox"  /> 
            <label class="marker_label">One Piece</label> 
        </div>
    </li>
    <li>
        <div class="markerDiv"  id="maker_school">
            <input class="marker_ckeckbox" name="markerType" value="DragonBall" type="checkbox"  /> 
            <label class="marker_label">Dragon Ball</label> 
        </div>
    </li>
    <li>
        <div class="markerDiv"  id="maker_school">
            <input class="marker_ckeckbox" name="markerType" value="Facts" type="checkbox"  /> 
            <label class="marker_label">Facts</label> 
        </div>
    </li>
    <li>
         <div class="markerDiv"  id="maker_school">
            <input class="marker_ckeckbox" name="markerType" value="Quotes" type="checkbox"  /> 
            <label class="marker_label">Quotes</label> 
        </div>
    </li>
    <li>
        <div class="markerDiv"  id="maker_school">
            <input class="marker_ckeckbox" name="markerType" value="Other" type="checkbox"  /> 
            <label class="marker_label">Other</label> 
        </div>
    </li>
</ul>

我正在考虑从 POST['tag1']['name'] 等中获取 $tag1,2...6 然后检查它们中的任何一个是否与其他没有任何不同 echo no category selected。虽然我想在 if 语句中插入名称为 $tag1,2,3...6 的标签的 t.id 和正在上传到 post_tags 的帖子的 postid?

我知道怎么用ph​​p写,但我只想知道思维过程是否正确。

【问题讨论】:

    标签: php sql database


    【解决方案1】:

    这称为 n 到 m 关系。你可以这样映射它

    posts table
    -----------
    id
    title
    ...
    
    
    tags table
    ----------
    id
    name
    ...
    
    
    post_tags table
    ---------------
    post_id
    tag_id
    

    要获取特定帖子的所有标签,您可以这样做

    select t.name
    from tags t
    join post_tags pt on pt.tag_id = t.id
    join posts p on pt.post_id = p.id
    where p.title = 'my little post'
    

    【讨论】:

    • 啊哈,我明白了,我不明白我将如何获取这些值以及将它们插入到哪里?我用一些 html 更新了这个问题,并猜测如果你能检查一下我会怎么做,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-23
    相关资源
    最近更新 更多