473,386 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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
Aug 28 '08 #1
8 45403
coolsti
310 100+
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.
Aug 28 '08 #2
nomad
664 Expert 512MB
You could capture their IP address using a cookie and then index that IP address for no dup.

nomad
Aug 28 '08 #3
Markus
6,050 Expert 4TB
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.  
Aug 28 '08 #4
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.
Aug 29 '08 #5
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.
Aug 29 '08 #6
Thanks for that code - but I found that it did not stop the script processing and submitting the data from the form to the DB - it just flashed up the message 'Username already exists' and carried on regardless!

The fix was simply to add 'exit' which causes the script to stop, only where the parameter (in my case I am checking email addresses) is already present in the DB, in which case the message that previously flashed up for a second now remains until the user goes back to the form and inserts a different email address - see below:

Expand|Select|Wrap|Line Numbers
  1. $email = $_POST['email']; // you must escape any input. Remember.
  2.  
  3. $query = "SELECT * FROM `register` WHERE `email` = '{$email}'";
  4.  
  5. $result = mysql_query($query);
  6.  
  7. if ( mysql_num_rows ( $result ) > 1 )
  8.  
  9. {
  10.  
  11.     /* Email already exists */
  12.  
  13.     echo 'That E Mail address already exists;
  14.  
  15.     exit;
  16.  
  17. }
Nov 24 '11 #7
Oops!

After testing this extensively I found that the code above (with 'exit;') does not pick up the first duplicate parameter - the form can be sublitted TWICE with the same email address - after that it shows the warning message.

The problem is again simple to fix - I changed this line:

Expand|Select|Wrap|Line Numbers
  1. if ( mysql_num_rows ( $result ) > 1 )
  2.  
  3. to:
  4.  
  5. if ( mysql_num_rows ( $result ) > 0 )
Makes sense when you think about it!
Nov 24 '11 #8
First add a good unique keys to username and e.g. to the email field and an auto increment primary key (auto incrementing user-id eg.)

To avoid unneccessary selects you could insert the new data with the ignore Attribute like

$sql = "INSERT IGNORE INTO [TABLENAME] SET [FIELDNAME] = '[VALUE]' ...";
$res = mysql_query($sql, $resource);

and then after sending the query, check the last_insert_id().

if( (int) mysql_last_insert_id($resource) == 0 )
{
die('username or email already taken');
}
Nov 26 '11 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: vasanth kumar | last post by:
Hi, I want to add a row to library table. Table has total 7 fields. But I want to create the new row with 5 field data. When I run the following query in MS Access strQ = "INSERT into library...
0
by: Gary Lundquest | last post by:
I have an application with MS Access 2000 as the front end and MySQL as the back end. All was well until I upgraded the MySQL (Linux) server. The Problem: I insert data into a cumulative table....
3
by: andreas.maurer1971 | last post by:
Hi all, since a few years I use the following statement to find duplicate entries in a table: SELECT t1.id, t2.id,... FROM table AS t1 INNER JOIN table AS t2 ON t1.field = t2.field WHERE...
3
by: AK | last post by:
Hi Our product uses MS-SQL Server 2000. One of our customer has 10 installations with each installation stroring data in its own database. Now the customer wants to consolidate these databases...
1
by: calebm12 | last post by:
Quick Question. I gotta a database with fields firstname, lastname, and hobby, etc. I dont want to allow duplicate entries for the name. For instance....no john smith twice....but there can be a...
5
by: Manish | last post by:
The topic is related to MySQL database. Suppose a table "address" contains the following records ------------------------------------------------------- | name | address | phone |...
8
by: desirocks | last post by:
Hi all, I am somewhat new to sql server. So help me in whatever way you can. I have two tables one of which doesnt have a primary key(table A) and other has a composite key formed of two...
7
by: php_mysql_beginer911 | last post by:
Hi .. i am trying to update a table where if field contents any duplictaed entries than one of the field should be updated with random number which is unique so i can make all entries unique i...
0
by: niths | last post by:
hi all, i had a users page in which the Admin can add users.so while adding a user i am able to restrict the duplicate entries(same names).Those users will be displayed in a table, and there is a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.