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? Link to comment https://forums.phpfreaks.com/topic/106422-insert-if-not-exist-and-then-update-another-table-with-id/ 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. Link to comment https://forums.phpfreaks.com/topic/106422-insert-if-not-exist-and-then-update-another-table-with-id/#findComment-545714 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. Link to comment https://forums.phpfreaks.com/topic/106422-insert-if-not-exist-and-then-update-another-table-with-id/#findComment-545801 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.