preventing duplicate row insertion in php/mysql

后端 未结 3 1245
生来不讨喜
生来不讨喜 2020-12-20 17:22

how do you prevent the entry of duplicate records in php mysql? validating input before insertion to the table.

i am currently building a user registration section f

3条回答
  •  一生所求
    2020-12-20 17:47

    Define unique constraints for the username and email columns:

    ALTER TABLE your_table ADD CONSTRAINT uk_username UNIQUE (username)
    ALTER TABLE your_table ADD CONSTRAINT uk_email UNIQUE (email)
    

    If the value attempting to be insertered or updated already exists in the table, MySQL will return an error stating the query violates the appropriate unique constraint (possibly both). It's up to you to setup PHP to handle this gracefully.

提交回复
热议问题