bamfon Posted January 13, 2011 Share Posted January 13, 2011 My primary key is not working its letting the same name "king" or anyother name i enter in to the database more then once :S eported database info CREATE TABLE IF NOT EXISTS `anime1` ( `id` int(15) NOT NULL AUTO_INCREMENT, `title` varchar(255) NOT NULL, `type` varchar(255) NOT NULL, `episode` varchar(255) NOT NULL, `year` varchar(255) NOT NULL, `genre` varchar(255) NOT NULL, `status` varchar(255) NOT NULL, `summary` mediumtext, `pictures` varchar(255) NOT NULL, PRIMARY KEY (`id`,`title`,`pictures`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ; -- -- Dumping data for table `anime1` -- INSERT INTO `anime1` (`id`, `title`, `type`, `episode`, `year`, `genre`, `status`, `summary`, `pictures`) VALUES (1, 'king', 'king', '', '', '', '', '', ''), (2, 'king', 'king', 'king', 'king', '', '', '', ''), (3, 'king', 'kingking', '', '', '', '', '', ''), (4, 'king', 'kingking', '', '', '', '', '', ''), (5, 'king', 'kingking', '', '', '', '', '', ''), (6, '', '', '', '', '', '', '', ''), (7, 'king', 'kingking', '', '', '', '', '', ''), (8, 'king', 'kingking', '', '', '', '', '', ''), (9, '<php ?>', 'kingking', '', '', '', '', '', ''); i am using normale [cod] $sql="INSERT INTO anime1 (title, type, episode, year, genre, status, summary, pictures) VALUES ('$title','$type','$episode','$year','$genre','$status','$summary','$pictures')"; [/code] Quote Link to comment https://forums.phpfreaks.com/topic/224271-primary-key/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 13, 2011 Share Posted January 13, 2011 PRIMARY KEY (`id`,`title`,`pictures`) That's a compost key, made up of those three fields. As long as your id is different in each row, the compost key made up of `id`,`title`,`pictures` is different. You would need to create a separate key just for the title. Quote Link to comment https://forums.phpfreaks.com/topic/224271-primary-key/#findComment-1158718 Share on other sites More sharing options...
bamfon Posted January 13, 2011 Author Share Posted January 13, 2011 Thank you and And how would i do that? Quote Link to comment https://forums.phpfreaks.com/topic/224271-primary-key/#findComment-1158720 Share on other sites More sharing options...
bamfon Posted January 13, 2011 Author Share Posted January 13, 2011 ok thank you i fixed it how do i make it so that if there is a Duplicate entry it show a text and not the normal Error: Duplicate entry '' for key 'PRIMARY' Quote Link to comment https://forums.phpfreaks.com/topic/224271-primary-key/#findComment-1158729 Share on other sites More sharing options...
BLaZuRE Posted January 13, 2011 Share Posted January 13, 2011 The query will fail since there is a duplicate key. Therefore, the query function will return false. Quote Link to comment https://forums.phpfreaks.com/topic/224271-primary-key/#findComment-1158987 Share on other sites More sharing options...
PFMaBiSmAd Posted January 13, 2011 Share Posted January 13, 2011 There are more than a few different reasons why a query will fail, not just due to a duplicate key. In your error checking and error reporting logic, you would need to test the value that mysql_errno returns and if it is the value for a duplicate key (which I think is 1062, but you should check to confirm), you would output your custom message and continue with your code that is inserting values. Quote Link to comment https://forums.phpfreaks.com/topic/224271-primary-key/#findComment-1159001 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.