Celeritas
Celeritas

Reputation: 15053

log into website

I'm designing a website where the user logs in. When creating accounts, I check to see if the username already exists. Should this checking be done inside the scripts or should I make the username a primary key of the table? I'm using MySQL and PHP.

So should I write a script that queries the database to check to see if the supplied username is already in use or should I try inserting the given user name and have the field as a primary key and if it fails it means that the user name is in use?

Upvotes: 0

Views: 79

Answers (1)

Mike Brant
Mike Brant

Reputation: 71384

I would take the approach of putting a unique constraint on the username field in the database, and then just trying an insert on the database. Don't check for the existing username first, as this is just an unnecessary query.

You can then evaluate the mysql response to determine if the insert failed due to duplicate key or whether something else went wrong.

Upvotes: 1

Related Questions