Delete table entry if not in current input
I have a form where users can enter tags like tag1,tag2,tag3. I explode that information and insert them into a table:tags one by one. Each tag in the table has a unique id (for referencing), a vid_id (for matching to a video), and a name (ex. tag1).
I want the behavior that if someone has those 3 tags in the table and then re-submits the form with tag开发者_开发知识库1,tag2 for the same vid_id that tag3 will be removed from the table. I was just thinking of a way to do this most efficiently and I was thinking of running a select statement each iteration or just deleting all the tags for that vid_id and reinserting tag1,tag2 but that seems inefficient. Anyone got any good ideas?
$variable=explode(',',$_POST['tags']);
foreach($variable as $tag) {
$id=md5(uniqid());
if ($tag!=''){
$sql="INSERT into tags (id,vid_id,name) VALUES (?,?,?)";
$stmt16 = $conn->prepare($sql);
$result=$stmt16->execute(array($id,$vid_id,$tag));
}
}
I don't fully understand the question. Pieces that might help you: WHERE NOT IN SQL syntax:
DELETE FROM tags
WHERE tags.name NOT IN ('tag1', 'tag2', 'tag3')
and REPLACE statement: http://dev.mysql.com/doc/refman/5.0/en/replace.html
精彩评论