 | 
August 28th, 2008, 08:04 AM
| | Newbie | | Join Date: Aug 2008
Posts: 22
| | 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
| 
August 28th, 2008, 08:19 AM
| | Needs Regular Fix | | Join Date: Mar 2008
Posts: 305
| |
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.
| 
August 28th, 2008, 05:55 PM
|  | Expert | | Join Date: Mar 2007 Location: CA.
Posts: 562
| |
You could capture their IP address using a cookie and then index that IP address for no dup.
nomad
| 
August 28th, 2008, 06:01 PM
|  | Moderator | | Join Date: Jun 2007 Location: York England :) Age: 18
Posts: 2,831
| |
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: -
<?php
-
-
$username = $_POST['username']; // you must escape any input. Remember.
-
-
$query = "SELECT * FROM `user_tbl` WHERE `username` = '{$username}'";
-
-
$result = mysql_query($query);
-
-
if ( mysql_num_rows ( $result ) > 1 )
-
{
-
/* Username already exists */
-
echo 'Username already exists';
-
}
-
else
-
{
-
/* Username doesn't exist */
-
/* .. insert query */
-
}
-
-
| 
August 29th, 2008, 01:11 AM
| | Newbie | | Join Date: Aug 2008
Posts: 22
| | 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: -
<?php
-
-
$username = $_POST['username']; // you must escape any input. Remember.
-
-
$query = "SELECT * FROM `user_tbl` WHERE `username` = '{$username}'";
-
-
$result = mysql_query($query);
-
-
if ( mysql_num_rows ( $result ) > 1 )
-
{
-
/* Username already exists */
-
echo 'Username already exists';
-
}
-
else
-
{
-
/* Username doesn't exist */
-
/* .. insert query */
-
}
-
-
| Problem solved. Thanks markusn00b.
| 
August 29th, 2008, 01:11 AM
| | Newbie | | Join Date: Aug 2008
Posts: 22
| | 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.
|  |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | 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.
|