SS Nath
SS Nath

Reputation: 75

How to give duplication validation in PHP mysql?

I have a field area_code in mysql table by php form. I need the validation & alert when typing the same area code which is already entered and stored in database.

Upvotes: 1

Views: 121

Answers (2)

Ivo Pereira
Ivo Pereira

Reputation: 3500

You could make a SELECT count statement and check if the returned rows. If so, it means that the record already exists.

SELECT COUNT(id) AS count FROM area_codes WHERE area_code = 'ABC'

If returned row is greater than 1, than the record you are trying to insert already exists.

Upvotes: 0

John Woo
John Woo

Reputation: 263693

The best way you do is to define a UNIQUE constraint on field area_code on the table.

ALTER TABLE tableName ADD CONSTRAINT tb_UQ UNIQUE (area_code)

if the code was executed and successful, the server will generate an error if you try to enter area_code that is already present on the table.

Upvotes: 2

Related Questions