开发者

How can I detect a duplicate post in PHP? [duplicate]

开发者 https://www.devze.com 2023-03-31 04:06 出处:网络
This question already has an answer here: How to prevent duplicating DB insertions when refreshing? (1 answer)
This question already has an answer here: How to prevent duplicating DB insertions when refreshing? (1 answer) Closed 10 months ago.

I have a form which inserts the text (of an article) into a database. When a user submits an article (that has already been submitted) it gets submitted also into the db.

How can I detect a duplicate post (like on stackoverflow)?

Should I select everything from the database and check if the submitted post is like a one before. (I want to post the 开发者_Python百科duplicate alert on the article page as the first comment)

Any help appreciated.

Thanks...


Create an unique ID, e.g. with uniq_id(), put it in a hidden field within your HTML form, then save it in the database in a field that is set to UNIQUE. When the INSERT operation fails, you know that the POST has already been sent before.


If You will have hidden id, that you get from db when try to edit some content, You can use code like this:

if (isset($_POST['id']) && trim($_POST['id']) != '') {
    $query = 'UPDATE table_name
                    SET
                      `key` = \'' .$_POST['key']  . '\'
                    WHERE `id` = \'' . $_POST['id'] . '\'', 0;
  } else {
    $query = 'INSERT INTO table_name
                                  SET
                                `key` = \'' . $_POST['key'] . '\'', 0;
  }
0

精彩评论

暂无评论...
验证码 换一张
取 消