1

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.

1
  • Why dont you just use a unique_key for area code and then catch the mysql_error? Commented Jan 22, 2013 at 9:46

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

It working. Thanks. But I need a alert pop-up box in the php form.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.