• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

PHP/MySQL - Check for duplicate rows

EpidemiK

Well-Known Member
NLC
Basically I have a user signup/login system on a MySQL backend. However, visitors can sign up multiple times with the same username. How would I get MySQL to check if there already exists the username in the database?

The table is like this

id username password email
1 blahblahb blahblah blah@blah.com
 
If your using phpMyAdmin you can simply click the "unique" button under the actions.
You can also run this query

Code:
ALTER TABLE `your_tbl` ADD UNIQUE (`your_username`)

just change "your_" to the actual names.
 
by the way, that will cause mysql to return an error msg when you try to insert an existing value. so it is a good practice to check if the field exist first in your php script before you do the actual insert.
 
Thanks for your help, but I just found out how. All I did was do
Code:
SELECT username FROM users WHERE username='$blah'
and count the number of rows. If it was >= 1, then it would return an error, else it would process it.
 
yeah. however my 1st post is a good backup like if somebody tried to hack the script, they still cant push thru coz mysql will block it.
 
Somenoe just tried to use SQL injection tactics on my site, and I just want to say thanks because you reminded me to fix it up. :)
 
Back
Top