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..