Norsk.Firefox Posted May 20, 2008 Share Posted May 20, 2008 Hello I have two tables: CREATE TABLE `tags` ( `id` int(11) NOT NULL auto_increment, `tag` varchar(40) collate utf8_unicode_ci NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM; And: CREATE TABLE `entries_tags` ( `id` INT NOT NULL AUTO_INCREMENT , `entry_id` INT NOT NULL , `tag_id` INT NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = MYISAM These are for my tag system.. But is it any way I can insert a tag into the "tags" table if it doesn't already exist, then get the ID and update the "entries_tags" table? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted May 20, 2008 Share Posted May 20, 2008 Do a SELECT on tags to see if it exists. If it doesn't, insert it. Use mysql_insert_id() to get the ID from the INSERT command. Then do an INSERT on the entries_tags table. Quote Link to comment Share on other sites More sharing options...
fenway Posted May 20, 2008 Share Posted May 20, 2008 INSERT... ON DUPLICATE KEY UPDATE... is the way to go. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.