Duplicate records added from post forms, easy solutions?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mindwarp

    Duplicate records added from post forms, easy solutions?

    Hi,

    When a user submits / posts data my php script Inserts data into my
    database.
    If they refresh the script or click back and click submit again I get
    duplicate record.

    Is there an easy solution to this?

    Or do I need to create a hidden field which contains some sort of
    unquie data, like a time stamp and have a have a unique field which
    stops it adding the record?

    Best regards,

    Jules.

  • Alvaro G. Vicario

    #2
    Re: Duplicate records added from post forms, easy solutions?

    *** mindwarp escribió/wrote (15 Jul 2006 02:31:04 -0700):
    If they refresh the script or click back and click submit again I get
    duplicate record.
    >
    Is there an easy solution to this?
    This simpliest solution is to move user away from insert script. After
    inserting data you can use header() to send a "Location" header and
    redirect user to another page (which can be even the same, but with an
    empty form).

    Of course, this is not bulletproof but should be enough.


    --
    -+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
    ++ Mi sitio sobre programación web: http://bits.demogracia.com
    +- Mi web de humor con rayos UVA: http://www.demogracia.com
    --

    Comment

    • Juliette

      #3
      Re: Duplicate records added from post forms, easy solutions?

      mindwarp wrote:
      Hi,
      >
      When a user submits / posts data my php script Inserts data into my
      database.
      If they refresh the script or click back and click submit again I get
      duplicate record.
      >
      Is there an easy solution to this?
      >
      Or do I need to create a hidden field which contains some sort of
      unquie data, like a time stamp and have a have a unique field which
      stops it adding the record?
      >
      Best regards,
      >
      Jules.
      >

      You could for instance:

      a) test the values received against the database before inserting, i.e.
      do a SELECT count(*) FROM table WHERE field1=$_POST[field1] AND
      field2=$_POST[field2] ...

      If the count if 1 or more, give an error message to the user and don't
      insert.

      b) make one or a combination of fields in your table unique which should
      make the insert fail. Be careful which combination you choose.


      Having a unique field in your table is always a good idea. Normally this
      would be a record-id field which auto-increments. This however will not
      prevent duplicate records from being entered as each record gets a new
      unique auto-incremented id.

      Having a timestamp as a unique field does not sound like a good idea.
      What about two users opening the same form page at the exact same moment
      ? (depending on how busy your site is, probably a very rare occurance,
      but still)
      It could work if your site does not have large volumes of traffic, but
      it is not a stable solution.

      Good luck,
      Juliette

      Comment

      • Jerry Stuckle

        #4
        Re: Duplicate records added from post forms, easy solutions?

        mindwarp wrote:
        Hi,
        >
        When a user submits / posts data my php script Inserts data into my
        database.
        If they refresh the script or click back and click submit again I get
        duplicate record.
        >
        Is there an easy solution to this?
        >
        Or do I need to create a hidden field which contains some sort of
        unquie data, like a time stamp and have a have a unique field which
        stops it adding the record?
        >
        Best regards,
        >
        Jules.
        >
        You could also set a session variable indicating the record had been
        added.

        --
        =============== ===
        Remove the "x" from my email address
        Jerry Stuckle
        JDS Computer Training Corp.
        jstucklex@attgl obal.net
        =============== ===

        Comment

        • Sheldon Glickler

          #5
          Re: Duplicate records added from post forms, easy solutions?

          "mindwarp" <mindwarpltd@gm ail.comwrote in message
          news:1152955864 .173058.229680@ p79g2000cwp.goo glegroups.com.. .
          Hi,
          >
          When a user submits / posts data my php script Inserts data into my
          database.
          If they refresh the script or click back and click submit again I get
          duplicate record.
          >
          Is there an easy solution to this?
          >
          Or do I need to create a hidden field which contains some sort of
          unquie data, like a time stamp and have a have a unique field which
          stops it adding the record?
          Generally I have a primary key and the "WHERE" clause prevents duplicates.
          However, to avoid errors you could check first for its existence and the
          either ignore it, send a message, or do an UPDATE.

          Shelly


          Comment

          • rik.catlow@gmail.com

            #6
            Re: Duplicate records added from post forms, easy solutions?

            You can add an if to check the database if the has the record already.

            $query_check = "SELECT * FROM `contest` WHERE record='$record '";
            $query = @mysql_query($q uery_check) or die("Could not check for
            account in our database.");

            if (mysql_num_rows ($query) 0) {
            $rv echo "That record is already in the database";
            }

            Well something like that...



            mindwarp wrote:
            Hi,
            >
            When a user submits / posts data my php script Inserts data into my
            database.
            If they refresh the script or click back and click submit again I get
            duplicate record.
            >
            Is there an easy solution to this?
            >
            Or do I need to create a hidden field which contains some sort of
            unquie data, like a time stamp and have a have a unique field which
            stops it adding the record?
            >
            Best regards,
            >
            Jules.

            Comment

            • s a n j a y

              #7
              Re: Duplicate records added from post forms, easy solutions?

              mindwarp wrote:
              Hi,
              >
              When a user submits / posts data my php script Inserts data into my
              database.
              If they refresh the script or click back and click submit again I get
              duplicate record.
              >
              Is there an easy solution to this?
              >
              Or do I need to create a hidden field which contains some sort of
              unquie data, like a time stamp and have a have a unique field which
              stops it adding the record?
              >
              Best regards,
              >
              Jules.
              >
              I would go with what Jerry said. Set a session variable indicating that
              insertion has been done. If user tries to repost, check for existence of
              the variable and just escape all the insertion code.

              Comment

              • Juliette

                #8
                Re: Duplicate records added from post forms, easy solutions?

                s a n j a y wrote:
                mindwarp wrote:
                >Hi,
                >>
                >When a user submits / posts data my php script Inserts data into my
                >database.
                >If they refresh the script or click back and click submit again I get
                >duplicate record.
                >>
                >Is there an easy solution to this?
                >>
                >Or do I need to create a hidden field which contains some sort of
                >unquie data, like a time stamp and have a have a unique field which
                >stops it adding the record?
                >>
                >Best regards,
                >>
                >Jules.
                >>
                >
                I would go with what Jerry said. Set a session variable indicating that
                insertion has been done. If user tries to repost, check for existence of
                the variable and just escape all the insertion code.

                I wouldn't advice that as that would also exclude a user from posting
                *something else* using the same form.

                Comment

                • Jerry Stuckle

                  #9
                  Re: Duplicate records added from post forms, easy solutions?

                  Juliette wrote:
                  s a n j a y wrote:
                  >
                  >mindwarp wrote:
                  >>
                  >>Hi,
                  >>>
                  >>When a user submits / posts data my php script Inserts data into my
                  >>database.
                  >>If they refresh the script or click back and click submit again I get
                  >>duplicate record.
                  >>>
                  >>Is there an easy solution to this?
                  >>>
                  >>Or do I need to create a hidden field which contains some sort of
                  >>unquie data, like a time stamp and have a have a unique field which
                  >>stops it adding the record?
                  >>>
                  >>Best regards,
                  >>>
                  >>Jules.
                  >>>
                  >>
                  >I would go with what Jerry said. Set a session variable indicating
                  >that insertion has been done. If user tries to repost, check for
                  >existence of the variable and just escape all the insertion code.
                  >
                  >
                  >
                  I wouldn't advice that as that would also exclude a user from posting
                  *something else* using the same form.
                  Why? When the form is first displayed, clear the session variable.
                  Then when the form is submitted, set the variable.

                  Pretty standard programming.

                  --
                  =============== ===
                  Remove the "x" from my email address
                  Jerry Stuckle
                  JDS Computer Training Corp.
                  jstucklex@attgl obal.net
                  =============== ===

                  Comment

                  Working...