stop duplicat entry in db

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kamleshghate2008
    New Member
    • Dec 2007
    • 3

    stop duplicat entry in db

    Hello

    I have a page on which i m placing order. After submitting the form, data is inserted to db successfully but when i refresh the page the duplicate data is inserted to db.

    How can i stop this.

    Can anybody help me?

    Thanx in advance
  • MarkoKlacar
    Recognized Expert Contributor
    • Aug 2007
    • 296

    #2
    Hi,

    could you post your SQL query?

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      Originally posted by kamleshghate200 8
      Hello

      I have a page on which i m placing order. After submitting the form, data is inserted to db successfully but when i refresh the page the duplicate data is inserted to db.

      How can i stop this.

      Can anybody help me?

      Thanx in advance
      [php]
      //mysql_num_rows sounds like your savior!

      //your mysql result
      $query = "some_query ";
      $query = mysql_query($qu ery);
      $result = mysql_num_rows( $query);
      if($result > 1){
      # data has already been inserted or page has been refreshed
      # don't run any queries to insert data!
      } else {
      # data is unique
      # run mysql queries to insert data
      }
      [/php]

      Comment

      • sejal17
        New Member
        • Dec 2007
        • 56

        #4
        Hello
        JUST TRY IT...

        After inserting data execute the query and then write:

        echo '<script language="javas cript">document .location="'.ba sename($_SERVER['PHP_SELF']).'";</script>'."\n";
        try this.
        then reply me.

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          Originally posted by sejal17
          Hello
          JUST TRY IT...

          After inserting data execute the query and then write:

          echo '<script language="javas cript">document .location="'.ba sename($_SERVER['PHP_SELF']).'";</script>'."\n";
          try this.
          then reply me.
          not what he's looking for; the user could just press the back button and refresh the page from there.

          And don't be so pushy.

          Comment

          • nitinpatel1117
            New Member
            • Jun 2007
            • 111

            #6
            The way that i stop this happening is by using the php redirect function.

            create a flag, i.e. a variable that is picked up using $_get or $post. if this flag is set then insert the sqll query into the database.
            after you have done the insert query succesfully, you can redirect to the same page again.

            when this page is reloaded it will lose the $_REQUEST variables, that were sent in the initial page request first time.

            therefore, the flag will not be set, and the insert query will not get executed


            Even if the person presses the back button on their browser. they will not be able to reinsert the data into the database as the brower thinks it is at the same url becuase of the php redirect.

            depening on whether you are using session variables, (I can't remember which way around it is)
            you may have to redirect to another page and then from this page redirect back to the original page.

            Comment

            Working...