thanks Andy. I make an index on the email field with a unique constraint but how do I supress a SQL error message if there is an INSERT that is not unique ?
Use the INSERT IGNORE statement to supress error messages. For example
INSERT IGNORE INTO my_table (name, email) values ('Fred', 'fred @mysite.com')
If you need to know if it was inserted, use the mysql_insert_id() to get the insert id, if it's equal to zero, nothing was inserted. For example :
$sql = "INSERT IGNORE INTO my_table (name, email) values ('Fred', 'fred@mysite.com')"; $result = mysql_query( $sql ); $insert_id = mysql_insert_id(); if ( $insert_id != 0 ){ echo "successfully inserted"; }else{ echo "errror with insert" }
ugh, I hate o be so stpid but how do you make a unique index ?
Use this schema instead of the one you have in the first post
CREATE TABLE `users` ( user_id int(11)NOT NULL auto_increment, first_name char(20) NOT NULL, last_name char(20) NOT NULL, sex char(1), email varchar(128) NOT NULL, PRIMARY KEY (`user_id`,`email`), UNIQUE KEY `idx_email` (`email`) ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1
|