PHP 中简单的重复单词检查器

发布于 2024-12-13 06:47:43 字数 423 浏览 0 评论 0原文

有没有办法检测使用 PHP 提交的重复表单?

我有一个文本字段,用于在其中输入一个单词短语(例如“Google”)。当用户按下提交按钮时,包含该文本字段的表单将被提交。 之后,在 PHP 处理页面上,从数据库中选择一些数据。如果提交的单词不存在,它将被插入到数据库中。但如果它已经存在,现有的就会获得一票赞成。这可能有点复杂,但我认为这是可以理解的。

唯一的问题是,当用户提交这样的内容时:“Google”。尽管已经有一条包含“Google”的记录,但它已被提交。

我的问题很简单: 如何检查重复项?

[我已经尝试过mysql功能:LIKE,但它并没有真正起作用,因为当有人提交这样的内容时:“Goofy”其中包含“Goo”,而该单词已经在单词中“谷歌”->已插入。 ]

那么我该如何解决这个问题呢?有什么好的脚本吗?

Is there anyway of detecting a duplicate form submit with PHP?

I have a textfield which is used for typing a one-word phrase into it (eg. "Google"). When the user presses the submit button, the form containing that textfield gets submitted.
After that - on the PHP processing page - some data gets selected from a database. If the submitted word doesn't exist, it gets inserted in to the db. But if it already exists, the existing one gets a vote up. This might be a little complicated, but I think it is understandable.

The only problem is that when a user submits something like this: "Google." it gets submitted, although there is already a record that contains "Google".

My question is simple:
How can I check for duplicates?

[I already tried out the mysql function: LIKE, but it didn't really work, because when someone submitted something like this: "Goofy" it contained "Goo" in it which is already in the word "Google" -> which is already inserted. ]

So how could I solve this? Is there any good script for it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

苦笑流年记忆 2024-12-20 06:47:44

虽然我同意 @Dustin 关于使用 INSERT ... ON DUPLICATE KEY UPDATE 的观点,但您可能希望在提交期间使用 javascript 或 PHP 在用户输入级别解决此问题,而不是让 SQL 尝试匹配琴弦。

目前输入字段有哪些限制?您是否希望用户使用标点符号、数字和其他常规非字母字符?

example of non-alpha: 1234567890-=()!@#$%^&*_+,./<>?;':"|\

如果是这种情况,我会为您提供 preg_replace 或 regExp 来删除任何这些不需要的内容人物。从而从源头消除问题。

验证/清理输入后,您可以使用简单的 SQL 插入,如下所示:

INSERT INTO table (string, count) VALUES (1,2)
   ON DUPLICATE KEY UPDATE count=count+1;

希望有所帮助。

While I agree with @Dustin about using the INSERT ... ON DUPLICATE KEY UPDATE you may want to address this issue at the user input level with javascript or PHP during submission rather than having the SQL try and match the strings.

What are the restrictions of the input field currently. Would you expect users to be using punctuation, number, and other general non-alpha characters?

example of non-alpha: 1234567890-=()!@#$%^&*_+,./<>?;':"|\

If this is the case I would you a preg_replace or a regExp to remove any of these unwanted characters. Thus removing the problem at is sources.

Once you have validated/cleansed the input you can use a simple SQL insert like so:

INSERT INTO table (string, count) VALUES (1,2)
   ON DUPLICATE KEY UPDATE count=count+1;

Hope that helps a little.

天气好吗我好吗 2024-12-20 06:47:44

您可以将数据库的名称列设置为唯一索引,这意味着不会写入任何重复项,并且您可以在插入 SQL 中检查该错误。

但在处理页面中简单地执行SELECT 'subscribedword' FROM table会更容易 - 如果没有结果,那么您就知道这是一个新单词,并且可以进行插入。如果它返回结果,那么您就执行增量函数。

You could set the name column of the database as a unique index, which would mean any duplicate would not get written and you could check for that error in your insert SQL.

But it would be easier in the processing page to simply do a SELECT 'submittedword' FROM table-- if no result, then you know it's a new word, and you can do the insert. If it returns a result, then you do your increment function.

嗳卜坏 2024-12-20 06:47:44

这似乎是 INSERT ... ON DUPLICATE 的一个很好的应用KEY UPDATE,其中您的密钥是唯一密钥,您的更新是数字列。

This seems like a good application of INSERT ... ON DUPLICATE KEY UPDATE, where your key is a unique key, and your update is the number column.

删除会话 2024-12-20 06:47:44

此帖子应该对您有帮助

它提供了 2 个解决方案,这两种解决方案都应该有效,具体取决于您是要修改 sql 查询还是使用 php 执行逻辑。

$result = mysql_query("update test set col='test' where col_id='1';");       
if (mysql_affected_rows()==0) {
    $result = mysql_query("insert into test (col_id, col) values ('1','test');");
}

您还应该考虑在不想重复的列上设置唯一索引。

它还提供: INSERT ON DUPLICATE KEY UPDATE< /代码>

This thread should help you.

It offers 2 solutions that should both work and depends on whether you want to modify your sql query or do the logic with php.

$result = mysql_query("update test set col='test' where col_id='1';");       
if (mysql_affected_rows()==0) {
    $result = mysql_query("insert into test (col_id, col) values ('1','test');");
}

You should also consider setting a unique index on the column that you don't want repeating.

It also offers: INSERT ON DUPLICATE KEY UPDATE.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文