简体   繁体   中英

How to avoid duplicates in a database from a textfield in a form

I am sorry, this question has probably been asked before but I can't find any satisfying answer so here I go:

I have a web form. The user can enter a company name in a text field. The content of this text field is then used to check if this company already exists in the database with a query like:

SELECT * FROM customers WHERE company_name='textfieldContent';

If the query returns no result, the company is added in the database. The problem is that if some people write the company name differently (eg 'Company' and 'Company Ltd.') or write it with a typo, the query returns no result and I end up having several rows in my database referring to the same company.

Does anyone have an idea on how to prevent this? Thanks.

The website was developed with Struts2 by the way.

Sounds like you need to add some front end validation to your web form to narrow things down a bit. You could instead of having a free text box have a drop down selection instead and then if the user cannot find the company name it gives them the option to add it in free text then.

You could code something to remove all the prefixes and suffixes but you would have a million edge cases that would ultimately be a pain to maintain.

If you are trying to search with exact company name then you can try this

SELECT * FROM customers WHERE company_name LIKE '%textfieldContent%' LIMIT 1

Or you can try this

SELECT * FROM customers WHERE company_name LIKE '%textfieldContent%'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM