View Full Version : PHP/MySQL - Check for duplicate rows
EpidemiK
December 18th, 2004, 11:24
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
kabatak
December 18th, 2004, 12:37
If your using phpMyAdmin you can simply click the "unique" button under the actions.
You can also run this query
ALTER TABLE `your_tbl` ADD UNIQUE (`your_username`)
just change "your_" to the actual names.
kabatak
December 18th, 2004, 12:40
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.
EpidemiK
December 18th, 2004, 14:23
Thanks for your help, but I just found out how. All I did was do
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.
kabatak
December 18th, 2004, 14:25
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.
EpidemiK
December 18th, 2004, 15:47
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. :)
Powered by vBulletin® Version 4.1.7 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.