Current location: Hot Scripts Forums » Programming Languages » PHP » duplicate entries


duplicate entries

Reply
  #1 (permalink)  
Old 05-12-09, 11:30 PM
reshma reshma is offline
Newbie Coder
 
Join Date: Mar 2009
Posts: 38
Thanks: 0
Thanked 2 Times in 2 Posts
duplicate entries

hi

I have a table with id,name and address. I do not want the user to store the same address more than once aganist his name. I tried a couple of things to prevent duplicate entries, but still not able to get any result. It would be great if someone could help me out with this.

Thanks !!!
Reply With Quote
  #2 (permalink)  
Old 05-13-09, 03:00 AM
mdhall's Avatar
mdhall mdhall is offline
Aspiring Coder
 
Join Date: Oct 2003
Posts: 510
Thanks: 1
Thanked 1 Time in 1 Post
Show us what you tried that didn't work so far.
Reply With Quote
  #3 (permalink)  
Old 05-13-09, 03:40 AM
reshma reshma is offline
Newbie Coder
 
Join Date: Mar 2009
Posts: 38
Thanks: 0
Thanked 2 Times in 2 Posts
duplicate enteries

i tried something like this using INSERT IGNORE

PHP Code:

mysql_query("INSERT  IGNORE INTO ListOfAdresses (VinNo, UserName, Nickname)VALUES('$vno', '$uname', '$name')") or die(mysql_error()); 

Reply With Quote
  #4 (permalink)  
Old 05-13-09, 11:38 AM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Quote:
Originally Posted by reshma View Post
hi

I have a table with id,name and address. I do not want the user to store the same address more than once aganist his name. I tried a couple of things to prevent duplicate entries, but still not able to get any result. It would be great if someone could help me out with this.
The simple ("crude") way of doing this is to make that column a UNIQUE column.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Reply With Quote
  #5 (permalink)  
Old 05-13-09, 02:04 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
WHen the user submits their data, query for the info you do not want duplicated, LIMITing to 1... and if a row is returned, you know that already exists... then tell them about it.
PHP Code:

// clean_post() -> pseudo magic_quotes cleansing function
$address clean_post'address' );

// build the query
$sql sprintf(
    
'SELECT `address_id` FROM `address` WHERE `address` = \'%s\' LIMIT 1',
    
mysql_real_escape_string$address )
);

// send the query
$result mysql_query$sql );

// $exists is TRUE if any rows are found, otherwise FALSE
$exists = ( bool ) mysql_num_rows$result );

if ( 
$exists )
{
    
// tell them there's a duplicate
}
else
{
    
// INSERT $address

I would also set a UNIQUE index on the column as End User suggested... juuust in case.
__________________
The toxic ZCE
Reply With Quote
  #6 (permalink)  
Old 05-14-09, 12:02 AM
reshma reshma is offline
Newbie Coder
 
Join Date: Mar 2009
Posts: 38
Thanks: 0
Thanked 2 Times in 2 Posts
duplicate entries

i tried using the unique key end user....the problem if i use UNIQUE is that it will not allow another user to enter the same address if one user has already entered it...i tried making the username also a unique key, but it dint show any results...

and i tried to use what u suggested keith, but i couldnt get it right, i dint quite understand the cleaning part of the code....
Reply With Quote
  #7 (permalink)  
Old 05-14-09, 02:10 AM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
Quote:
Originally Posted by reshma View Post
and i tried to use what u suggested keith, but i couldnt get it right, i dint quite understand the cleaning part of the code....
__________________
The toxic ZCE
Reply With Quote
  #8 (permalink)  
Old 05-14-09, 03:23 AM
reshma reshma is offline
Newbie Coder
 
Join Date: Mar 2009
Posts: 38
Thanks: 0
Thanked 2 Times in 2 Posts
duplicate entries

what i meant was that i dint understand why u are doing clean_post() and the escape string part....and i dont really know what these functions do....sorry about the previous thread...
Reply With Quote
  #9 (permalink)  
Old 05-14-09, 07:46 AM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
Pseudo means fake. It's just an example. I certainly hope you're not copying and pasting example code snippets into your application.

The cleanse_post() function could be anything means that sanitizes the input, including removing any slashes added when magic_quotes is enabled, making it suitable for display.

PHP: mysql_real_escape_string - Manual
__________________
The toxic ZCE
Reply With Quote
  #10 (permalink)  
Old 05-14-09, 09:52 PM
reshma reshma is offline
Newbie Coder
 
Join Date: Mar 2009
Posts: 38
Thanks: 0
Thanked 2 Times in 2 Posts
duplicate entries

thanks a lot !!!! i am definitely not copying and pasting the example code mentioned.... thanks again...
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Prevent Duplicate SQL Entries DHD-Nick PHP 5 05-12-09 03:06 PM
update/remove duplicate entries (mysql, php) negru Script Requests 1 10-24-08 01:49 PM
Counting duplicate entries in an array. MGCJerry PHP 3 03-24-06 01:22 AM
MySQL: Can it handle 1 million + entries? archangel PHP 3 08-16-04 03:52 AM
LOTS of duplicate entries KingSky HotScripts Site Bug Reports 0 06-17-03 10:09 AM


All times are GMT -5. The time now is 08:35 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.