|
Pages: [1] |  |
|
|
Author
|
Topic: Insert unique record in mysql ? (Read 2222 times)
|
|
| |
|
 Joined: Jul 2007 Posts: 28

United States
|
Sep 17, 2007, 05:14:28 AM | #2 |
| You can make the primary key on the email field or make a unique index and any inserts will fail. |
|
|
|
|
|
|
|
|
    Joined: Jan 2006 Posts: 6215
Administrator
 
 United States
|
Oct 15, 2007, 08:03:32 PM | #5 |
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
|
|
Latest Blog Post :
8 Tips for Creating a Marketing Buzz
|
|
|
 Joined: Sep 2011 Posts: 71

India
|
Sep 16, 2011, 05:26:05 AM | #6 |
| Just check from the Database that email is already available on your Databse with the Coding..Just Compare the database records with the new record of client..if the record is available already then you should display the message email is alreaDY exists.. |
|
|
|
|
 |
|
Pages: [1]
|
|
|
 |