开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜