How to prevent double form submit on the php or mysql level?

php mysql

36 观看


1 作者的声誉

I know I can do disable the button via js, but the user can easily alter that in his browser.

So, I am looking for an easy solution how to do that.

I have tried this SESSION trick however, I can still click fast and it will save many things in the db.

Also, if I do the does a row with the same user_id, item_id and comment_id in the table reviews exist then stop condition -> this doesn't work either, when I click very fast on the button.

Does mysql have some function to refuse saving more than one row with the same info?

If something like that exists that would be great.

For example user_id, item_id, comment_id can be save only once or perhaps using some time prevention to save multiple things.

作者: Greggore 的来源 发布者: 2017 年 12 月 27 日

回应 1


1773 作者的声誉

Design your data such that multiple submissions will not result in multiple entries.

Here are 2 ways to accomplish that, I prefer the second.

[Unique columns]1 and the @ sign to suppress errors in php. <- this results in the most recent submission being inserted

Or better yet you can do a call to the DB and see if the information already exists and if so, dont make the insert call. <- this results in the first submission being inserted. Conditional logic can segue to an update call if that is appropriate.

作者: NappingRabbit 发布者: 2017 年 12 月 27 日