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

Recommended Answers

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

Jump to Post

All 2 Replies

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

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)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.