Prevent Duplicate SQL entries

谁都会走 提交于 2019-11-29 11:52:33
CREATE UNIQUE INDEX idxname ON tablename (fieldname);

Adding this index will ensure that no duplicate entries for fieldname field will be recorded into tablename table.

You will get a MySQL error with the second client. You should handle this in your PHP code, and put up the form again (instead of just displaying the error message).

An other possibility (for more complex sitations) is the LOCK functionality. If you lock the table before checking and then you insert your record a concurrent operation (in the second browser window) will be delayed until you release the locks. Then the record will be already saved, so the second PHP script will see it and handle the sitation.

DISTINCT can also be used to select unique rows.

...
select distinct *
  from table1
...

Take a look here, you'll need unique.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!