Webmaster Help
|
Welcome, Guest. Please login or register.
Did you miss your activation email?
Jan 05, 2009, 08:39:15 PM

Login with username, password and session length

Genius is 99 percent perspiration and 1 percent inspiration.


- Thomas Edison

Welcome Guest - Register or Login Now to remove these advertisments


Registration is completely free and only takes a few minutes to signup !

|-   Entrepreneur Forum > Webmasters - Discuss Making Money Online > Website Design and Maintenance > Database
+   Insert unique record in mysql ?
0 Members and 1 Guest are viewing this topic.
Pages: [1] Reply to Thread
Author Topic: Insert unique record in mysql ?  (Read 480 times)
OfflinetopProducer
Junior Member
topProducer is on a distinguished road
Joined: Sep 2007
Posts: 61


anonymous avatar

View topProducer\s Profile
United States
notepad Sep 03, 2007, 12:20:07 PM #1
How do I insert in the mysql a unique value for email.

my table is schema of this

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)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1

I want use php to insert to mysql but im wanting unique on the email field. by hand i work to remove duplicate each day and is long work some day.

how can I do this ?
OfflineAndyColemants
New Member
AndyColemants is on a distinguished road
Joined: Jul 2007
Posts: 28



View AndyColemants\s Profile
Cancer United States
notepad 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.

OnlineGalaxian
Senior Member
Galaxian is on a distinguished road
Joined: Oct 2007
Posts: 238


Avatar of Galaxian

The internet is my playground.

View Galaxian\s Profile
Gender: MaleAries United States
notepad Oct 15, 2007, 02:21:53 PM #3
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 ?

Latest Blog Post : The Advantages of Water-Powered Car Technology

Got an eye for The Best Cars like a sizzling Ferrari sports car or the luxury of BMW?
OfflinetopProducer
Junior Member
topProducer is on a distinguished road
Joined: Sep 2007
Posts: 61


anonymous avatar

View topProducer\s Profile
United States
notepad Oct 15, 2007, 02:29:27 PM #4
ugh, I hate o be so stpid but how do you make a unique index ?
Online~Dave~
Administrator
~Dave~ is a jewel in the rough~Dave~ is a jewel in the rough~Dave~ is a jewel in the rough~Dave~ is a jewel in the rough
Joined: Jan 2006
Posts: 6186


Avatar of code4gol

Building a Foundation

View ~Dave~\s ProfileWWW
Gender: MaleGemini United States
notepad Oct 15, 2007, 08:03:32 PM #5
Quote from: Galaxian on Oct 15, 2007, 02:21:53 PM
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 :

Code:
$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"
}

Quote from: topProducer on Oct 15, 2007, 02:29:27 PM
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

Code:
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 : Social Media Optimization for Social Media Marketing

Pages: [1] Reply to Thread


Code4Gold Affiliate Entrepreneur Moneymaker Forum © 2006-2008 Resdaz Media LLC - All Rights Reserved

Forum Software Powered by SMF - © 2001-2008, Lewis Media. All Rights Reserved.