Duplicate records problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gardouth
    New Member
    • Mar 2008
    • 2

    Duplicate records problem

    Hi, I'm building a basic forum type thing but having a problem with getting records duplicated when people reply to a post.

    This is the code I'm using to process the replies...[php]
    $thread_id = $_POST["thread_id"];
    $details = strip_tags($_PO ST["details"], "<b><i><u>" );
    $member_id = $_POST["member_id"];
    mysql_query("IN SERT INTO forumReplies (Details, MemberID, ThreadID) VALUES ('" . $details . "', '" . $member_id . "', '" . $thread_id . "')");
    header('Locatio n: details.php?id= ' . $thread_id);[/php]It works, but the problem is that SOME (apparently at random), get repeated when the user hits submit, but some don't. I can't work out any reason for it. It seems that the code is being run twice on some occations.

    I looked at the data in the database and it does indeed post two exact copies of the same post (with different record ID's).

    I have recently updated to MySQL from an Access driven version. I also had exactly the same problem with Access (similar ASP code) but assumed that it was just crappy Access. It seems not.

    Any help is really apreciated!
    Last edited by ronverdonk; Mar 27 '08, 12:59 AM. Reason: code tags
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Welcome to The Scripts! BUT:

    Please enclose your posted code in [code] tags (See How to Ask a Question).

    This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

    Please use [code] tags in future.

    MODERATOR
    Last edited by ronverdonk; Mar 27 '08, 01:00 AM. Reason: typo

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      Can it be that someone hits the submit button twice or someone refreshes the page? That would be a cause and you will have to guard against that.

      By the way: you can write that query easier this way (as long as it is between double quotes):[php]mysql_query("IN SERT INTO forumReplies (Details, MemberID, ThreadID) VALUES ('$details', '$member_id', '$thread_id')") ;[/php]
      Ronald

      Comment

      • gardouth
        New Member
        • Mar 2008
        • 2

        #4
        Sorry about not using CODE tag, I will make sure I do in the future.

        Thanks for the code advice. Hitting the button twice was a thought I had too, but it happens when I try it too and I'm definatly only submitting once. I have some javascript that disables the submit button after submit. It could be submitting twice (which would make sence) without me realising it.

        Can anyone suggest a better way to prevent a form being submitted more than once?

        Comment

        • ronverdonk
          Recognized Expert Specialist
          • Jul 2006
          • 4259

          #5
          As I said, a refresh of the page or a page-back and refresh could do the same.

          Ronald

          Comment

          Working...