1,105,271 Community Members

inserting duplicate record should not allowed

Member Avatar
noel9
Newbie Poster
16 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

how do i stop user inserting duplicate records

INSERT INTO person_tbl(person_name,person_status,creation_time,modified_time) VALUES ('$person',$status,$ctime,$mtime)
WHERE person_name NOT IN(SELECT person_name 
FROM person_tbl
WHERE city_name='$person');
Member Avatar
pritaeas
mod_pritaeas
11,285 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,829 [?]
Skill Endorsements: 154 [?]
Moderator
Featured
Sponsor
 
1
 

Set a unique index on person_name in your table. If you try to insert a duplicate, the query will throw an exception.

Member Avatar
rch1231
Veteran Poster
1,182 posts since Sep 2009
Reputation Points: 131 [?]
Q&As Helped to Solve: 193 [?]
Skill Endorsements: 13 [?]
 
1
 

Hello,

Pritaeas is correct about the index and you will need to change the insert statement and add IGNORE to cause mysql to ignore the errors when the insert fails due to duplicates.

INSERT IGNORE INTO person_tbl(person_name,person_status,creation_time,modified_time) VALUES ('$person',$status,$ctime,$mtime)

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article