I was hoping someone will be able to help me with the following. I've got a table that looks like this:
As you can see it has two unique keys and a primary key. I've added the unique keys because I want the customerid and the e-mail address to be unique.Code:CREATE TABLE `user` ( `userid` mediumint(9) NOT NULL auto_increment, `email` varchar(255) NOT NULL default '', `customerid` varchar(255) NOT NULL default '', PRIMARY KEY (`userid`), UNIQUE KEY `customerid` (`customerid`), UNIQUE KEY `email` (`email`) ) TYPE=MyISAM AUTO_INCREMENT=1 ;
In most situations my tables only have one unique key, so I can something like this to catch the unique error and handle it:
But how would I do this with two unique keys, and be able to know which key triggered the unique error?PHP Code:$db->exec ("INSERT INTO table (field1, field2) VALUES ('one', 'dup')");
if ($db->errno == 1062) {
echo 'Already exists, please go back and try a different one';
}
// ...
MySQL does return the key number, like so:
But that isn't really helpful, since I have no real way of knowing what key 3 or key 2 is. I'd rather have the actual fieldname. Another problem is that I'd have to parse the error message.Code:Duplicate entry 'foo' for key 3
Any suggestions or tips? Or should I just hard code the key 3 and key 2 in my script?




Bookmarks