Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

test if username exists

Status
Not open for further replies.

tinapa

Technical User
Nov 12, 2008
81
GB
hi guys, do you know how to test if a username is already existing in the database? i am using asp, sql server, dreamweaver.


sampletable1

id username password
-- --------- ---------
1 username1 password1
2 username2 password2
.
.
.

or do you have any sample codes similar to this?

thnx
 
You could try something like this:

'setup connection to db


'create record set

Set rs = Server.CreateObject("ADODB.Recordset")
sql= "Select username from users where username=" & newusername

If NOT rs.EOF Then
Response.write " Please choose another username."

Else

'Add recored to db

End If
'Clean up
 
or this:

'setup connection to db we'll use conn as an example


sql= "Select username from sampletable1 where username=" & newusername

set rs= conn.execute(sql)

If NOT rs.EOF Then
Response.write " Please choose another username."

Else

'Add recored to db

End If
'Clean up

Be sure to validate user input to check for injection or cross site scripting before using the newusername parameter.
 
cr84net2 thnx.

i will look into that
 
Yeah... pretty basic stuff....

But you *DEFINATELY* want to sterilize the input before bouncing it off of your user database like that.....

.... or use a stored procedure.



Just my 2¢

"What the captain doesn't realize is that we've secretly replaced his Dilithium Crystals with new Folger's Crystals."

--Greg
 
stored procedure is definitely the way to do DB access:

1. call the stored procedure that you create passing through the Username as a parameter.

2. in the stored procedure do:
IF EXISTS (SELECT UserID FROM tUsers WHERE Username = @Username)
RETURN -1
ELSE
RETURN 0

3. in your asp you can then get the return parameter from the stored procedure and do what you want.

However, you should also think about letting the stored procedure do all the "business" stuff, so that you don't have to keep going back and forth between asp and db, for performance
 
thanks SimonEireconsulting, i will look into that
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top