Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old August 28th, 2008, 08:04 AM
Newbie
 
Join Date: Aug 2008
Posts: 22
Default How can I avoid adding duplicate entries into database table.

Hi guys,

I have a problem. I have created a login system where user can register their login details before they login to website, to register they have to provide there username,password and email address but I have problem that I can register same user thousand times. How can I stop registering same username second time. Any help will be appreciated.

Thanks
Reply
  #2  
Old August 28th, 2008, 08:19 AM
Needs Regular Fix
 
Join Date: Mar 2008
Posts: 305
Default

You can basically do this in a few ways.

Your database tables which store the user information, such as user name and perhaps email address, etc., should be set up with a unique key so that the same user could not be added to the table twice. For example, if you identify a user by name + email address, you would set up the table so that these two columns together are a unique index. From my experiences with various sites, however, it seems that either the user name (i.e. screen name) or email address alone is used as the unique index.

Setting up the database to not allow a second entry using unique indexes is how you enforce that someone only registers once. But you also wish to show this to the user in a user-friendly manner. So what you can do when someone attempts to register is to first check the database to see if that user already exists, and if so, you send back a page with an error message saying that the user is already registered.

If you set up your database table with unique indexes, you could perhaps simply try to perform the insert of the user when he/she tries to register. If there is a duplicate key violation, the insert will not occur, and your database will return with an appropriate error code (1062 for MySQL). You could then have your script examine the error code and if it is the one for a duplicate key error, you then can send the error message of already being registered to the user.
Reply
  #3  
Old August 28th, 2008, 05:55 PM
nomad's Avatar
Expert
 
Join Date: Mar 2007
Location: CA.
Posts: 562
Default

You could capture their IP address using a cookie and then index that IP address for no dup.

nomad
Reply
  #4  
Old August 28th, 2008, 06:01 PM
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York England :)
Age: 18
Posts: 2,831
Default

I'm sure this was covered in the same post you made a day or two ago.

You run a 'where' clause in a mysql query. Using the result of this query and a mysql_num_rows() check you can find whether the username is already present in the database.

Take this for example:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $username = $_POST['username']; // you must escape any input. Remember.
  4.  
  5. $query = "SELECT * FROM `user_tbl` WHERE `username` = '{$username}'";
  6.  
  7. $result = mysql_query($query);
  8.  
  9. if ( mysql_num_rows ( $result ) > 1 )
  10. {
  11.     /* Username already exists */
  12.     echo 'Username already exists';
  13. }
  14. else
  15. {
  16.     /* Username doesn't exist */
  17.     /* .. insert query */
  18. }
  19.  
  20.  
Reply
  #5  
Old August 29th, 2008, 01:11 AM
Newbie
 
Join Date: Aug 2008
Posts: 22
Default

Quote:
Originally Posted by markusn00b
I'm sure this was covered in the same post you made a day or two ago.

You run a 'where' clause in a mysql query. Using the result of this query and a mysql_num_rows() check you can find whether the username is already present in the database.

Take this for example:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $username = $_POST['username']; // you must escape any input. Remember.
  4.  
  5. $query = "SELECT * FROM `user_tbl` WHERE `username` = '{$username}'";
  6.  
  7. $result = mysql_query($query);
  8.  
  9. if ( mysql_num_rows ( $result ) > 1 )
  10. {
  11.     /* Username already exists */
  12.     echo 'Username already exists';
  13. }
  14. else
  15. {
  16.     /* Username doesn't exist */
  17.     /* .. insert query */
  18. }
  19.  
  20.  
Problem solved. Thanks markusn00b.
Reply
  #6  
Old August 29th, 2008, 01:11 AM
Newbie
 
Join Date: Aug 2008
Posts: 22
Default

Quote:
Originally Posted by coolsti
You can basically do this in a few ways.

Your database tables which store the user information, such as user name and perhaps email address, etc., should be set up with a unique key so that the same user could not be added to the table twice. For example, if you identify a user by name + email address, you would set up the table so that these two columns together are a unique index. From my experiences with various sites, however, it seems that either the user name (i.e. screen name) or email address alone is used as the unique index.

Setting up the database to not allow a second entry using unique indexes is how you enforce that someone only registers once. But you also wish to show this to the user in a user-friendly manner. So what you can do when someone attempts to register is to first check the database to see if that user already exists, and if so, you send back a page with an error message saying that the user is already registered.

If you set up your database table with unique indexes, you could perhaps simply try to perform the insert of the user when he/she tries to register. If there is a duplicate key violation, the insert will not occur, and your database will return with an appropriate error code (1062 for MySQL). You could then have your script examine the error code and if it is the one for a duplicate key error, you then can send the error message of already being registered to the user.
I am able to fix the problem. Thanks coolsti.
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles