Ad Code

PHP MySQL Insert Query that won't insert if already exists

Solution 1:

Create a distinctive index for the  (user, label, empid)  . As a result, the database will prohibit the creation of any duplicate entries.

ALTER TABLE pms_users
ADD UNIQUE INDEX (user, label, empid);



Solution 2:

ALTER TABLE pms_users ADD CONSTRAINT pms_users_unq UNIQUE (`user`, `label`, `agent`);

INSERT IGNORE INTO `pms_users`
(`user`, `label`, `agent`, `empid`)
VALUES ('some_user', 'some_label', 'some_agent', 123)

 


Post a Comment

0 Comments