Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 UNIQUE index and duplicate error

Author  Topic 

fluvly
Starting Member

2 Posts

Posted - 2009-01-15 : 08:52:09
Hi everyone,

I have just created a UNIQUE index on a "username" field. Obviously, when I try to register a new user with an already existing username, the query doesn't succeed.

What I would like to know is whether there is a way for php to detect that the query doesn't succeed for that precise reason and not for a server issue... so that it would reload the "user registration" page with the error "username already in database" written on top. How can I achieve that?

Thank you for any help.

Flaviana

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-15 : 08:56:04
just check before insertion if username already exist and raiserror if it does like

IF EXISTS(SELECT 1 FROM table WHERE username=@yourvalue)
RAISERROR ('username already in database',10,1)
ELSE
insert code here...
Go to Top of Page

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2009-01-15 : 08:56:20
you can have sql to check if userid is already in the table
something like,

if exists(select 1 from yourtable  where username=<username>)
print 'It already exists'
else
....
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2009-01-15 : 08:56:59
You'll have to capture it manually...something like this might work:
IF EXISTS (SELECT Username FROM table WHERE Username = @username)
RAISERROR('Username allready exists', 16, 1)
ELSE
INSERT INTO table () ...


- Lumbago
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2009-01-15 : 08:58:13
OMG! 3 almost identical replies within one minute!



- Lumbago
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-15 : 09:01:08
quote:
Originally posted by Lumbago

OMG! 3 almost identical replies within one minute!



- Lumbago


Wow! I'm winner by 16 s
Go to Top of Page

fluvly
Starting Member

2 Posts

Posted - 2009-01-15 : 09:07:28
Thank you all very much!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-15 : 09:08:44
welcome
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2009-01-15 : 09:13:14
If I only didn't test mine first...bummer!! Hehe, I couldn't remember if the level/state of raiserror came before or after the message

- Lumbago
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-15 : 23:54:11
Go to Top of Page
   

- Advertisement -