简体   繁体   English

如何避免表单中文本字段在数据库中的重复

[英]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:对不起,这个问题之前可能已经被问过,但我找不到任何令人满意的答案,所以我在这里 go:

I have a web form.我有一个 web 表格。 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.问题是,如果某些人以不同的方式书写公司名称(例如“Company”和“Company Ltd.”)或使用拼写错误,则查询不会返回任何结果,并且我的数据库中最终会有几行引用相同的公司。

Does anyone have an idea on how to prevent this?有谁知道如何防止这种情况? Thanks.谢谢。

The website was developed with Struts2 by the way.该网站是顺便用Struts2开发的。

Sounds like you need to add some front end validation to your web form to narrow things down a bit.听起来您需要在 web 表单中添加一些前端验证,以缩小范围。 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%'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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