Sana Sana
Sana Sana

Reputation: 11

how to prevent duplicate in database?

I need to compare a value typed into a form field against a list queried from a database to ensure duplicate information is not submitted before submit form

Upvotes: 0

Views: 91

Answers (2)

nalinc
nalinc

Reputation: 7425

You may trigger an ajax call on 'field change' and check if you get any resultset for that particular value in database. Alternatively, you may do this right before form submission.

Have a look at this tutorial on checking user availability asynchronously.

Also, you should make the field unique in your database table to avoid any accidental duplication.

ALTER TABLE TableName ADD UNIQUE (columnName)

Hope it helps, cheers!

Upvotes: 1

YyYo
YyYo

Reputation: 641

If you means, checking if user input is already exists in database BEFORE submitting it to server ? You can send all user input via AJAX request, for checking if input already exists, and according to AJAX response to determine if to submit it or not.

IMO, its advice to send it directly to server, and for the server to return a response if the data insert successfully or already exists.

Note: you can do some input validation, but its up to you.

Hope it help a bit

Upvotes: 1

Related Questions