duanquezhan7268 2012-03-18 14:47
浏览 6
已采纳

开发标记系统 - 如何使用新标记更新当前标记?

I am designing a Tagging system for my site(a customized blog). These are my source codes

<b>Please enter your tags, separating each one with a comma.<b><br>
<input type="text" name="tags" size=50>

$taginput = $_POST["tags"];
$tagarray = explode(",",$taginput);
for($i=0;$i<count($tagarray);$i++){
$usetag = mysql_real_escape_string(stripslashes(ltrim(rtrim($tagarray[$i]))));
if($usetag == "") continue;
$query = "INSERT INTO tags (item_id,tag) VALUES ($itemid,'$usetag')";
mysql_query($query);
}

CREATE TABLE `tags` (
`uid` INT( 11 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`item_id` INT( 11 ) NOT NULL,
`tag` VARCHAR( 50 ) NOT NULL
) ENGINE = MYISAM ;

Where item_id is the blog post id and tag is the tag name

Now Im looking for a solution on how if I update a post with tags.. how can i delete the present tags and replace it with the new tags? and how can i delete some specific present tags and replacing it with new ones?

Like this,

Tags: shirt, apparel, arrivals replaced by Tags: t-shirts, old, sale

and

Tags: shirt, apparel, arrivals to Tags: shirt, old, arrivals

I also want to prevent overpopulating the tags tables with duplicate tag names if possible. Please help..

  • 写回答

3条回答 默认 最新

  • douzhunlan5930 2012-03-18 15:41
    关注

    To prevent duplicate tags you need another table for the many to many relationship

    create table `items_tags` (
        `item_id` int(10) unsigned not null default 0,
        `tag_id` int(10) unsigned not null default 0,
        primary key (`post_id`, `tag_id`),
    )
    
    create table `tags` (
        `id` int(10) unsigned not null auto_increment,
        `tag` varchar(100) not null default '',
        primary key (`id`),
        unique key (`tag`)
    )
    

    ps - an unsigned int is only 10 characters in length, not 11.

    I'm not gonna type out all the code for you but when the form is submitted you "INSERT IGNORE" on the tags table, select the id's where tag IN( ), delete from items_tags where item_id = and finally insert into items_tags a record for each tag id with the current item id.

    I suggest you start learning pdo. It make things much easier. You should also have foreign key constraints with cascade on delete so when an item or tag is deleted it's references in the items_tags table are also deleted.

    To retrieve all tags for a post is simple:

    select i.*, t.* from items as i left join items_tags as it on (it.item_id = i.id) left join tags as t on (it.tag_id = t.id) where i.id = <current item id>
    

    Hope that helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端