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');

Edited by pritaeas: Added markdown.

3
Contributors
2
Replies
20
Views
3 Years
Discussion Span
Last Post by rch1231
1

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

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)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.