Avoid Duplicate Inserts

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default Avoid Duplicate Inserts

    Hi All,

    I?m trying to do a normal INSERT in to a SQL server database. I take the
    values from a form and pass it on to another page and calling a stored
    procedure, which does the inserts (only one ISERT statement). I submit the page
    only once but two records got inserted with the same values. I have the same
    date time value for both rows and both are same, even the seconds are same.

    Why is it inserting multiple records with same values in single click? How do
    I avoid it? Please advice. Any help is greatly appreciated.

    Many thanks / Manu.

    wmanu Guest

  2. Similar Questions and Discussions

    1. Duplicate Record Inserts
      The problem is that records are not being inserted only once. The included code is processed in a page after a form. The following 3 events randomly...
    2. SQL INSERTS in Dreamweaver ASP?
      :confused; I am new to dreamweaver.asp. I was told that it allowed interactions with a SQL database, and that this was the product to choose for...
    3. 1-to-Many Inserts
      I have a form which collects 1 article, but has many authors. The form goes to the action page fine, and I'm collecting the authors via a <select>...
    4. CLOB Inserts
      Has anyone had problems trying to INSERT very long text strings into CLOB columns in a table? How do you insert text > 4k bytes in length into a...
    5. [PHP] verifying sql INSERTs
      Marek Kilimajer <mailto:kilimajer@webglobe.sk> on Tuesday, August 26, 2003 3:59 AM said: OOhhh... You know, I already HAVE done this, and I...
  3. #2

    Default Avoid Duplicate Inserts

    Hi All,

    I?m trying to do a normal INSERT in to a SQL server database. I take the
    values from a form and pass it on to another page and calling a stored
    procedure, which does the inserts (only one INSERT statement). I submit the
    page only once but two records got inserted with the same values. I have the
    same date time value for both rows and both are same, even the seconds are
    same.

    Why is it inserting multiple records with same values in single click? How do
    I avoid it? Please advice. Any help is greatly appreciated.

    Many thanks / Manu.

    wmanu Guest

  4. #3

    Default Re: Avoid Duplicate Inserts

    Well, it does sound very much like you are unaware of the fact that you double
    click the submit button... I encountered a similar situation once and after a
    long time of debugging I found out it happened because of users who doubleclick
    the submit button. But that was with ASP.. I never saw it with CF.

    Could you show the code, so we can take a look...

    pukkel Guest

  5. #4

    Default Re: Avoid Duplicate Inserts

    Hi,

    Thanks for your input. Unfortunately I do not have the exact code at the
    moment but will get you tomorrow. For your reference, its just a normal form
    with text fields and on submit it goes to another page, which calls the SP and
    do the insert and then CFLOCATION to another page where all the records are
    displayed. I hope this make sense.

    I?m aware that F5 or more than one submit can create duplicate rows. So I?m
    not doing them and I?m the only one who operates these pages. If I try
    inserting from faster machine it works well, I mean no rows are getting
    duplicated.

    But if I try from my laptop, which is a bit slow (AMD850, 256RAM), it adds 2
    records with same data almost all the time. Even in that one or two records got
    inserted with out duplication.

    I experienced it several times; only now I took a chance to discuss with you
    guys.

    Thanks a million for your time. Any help is greatly appreciated.

    Thanks / Manu.

    wmanu Guest

  6. #5

    Default Re: Avoid Duplicate Inserts

    Just curious, but if it's a simple insert why are you using a SP?
    OldCFer Guest

  7. #6

    Default Re: Avoid Duplicate Inserts

    I have the same problem with my code, how it is possible to avoid more then one
    entry of the same record?

    I would also add this second scenario, a user will try to add an identical
    record later in time for the simple reason the stress my application... in this
    case how do I prevent double entries?

    I shold run a query first the check if the same record I want to enter is
    already present but I have no idea of the code. Any help will be appreciated.

    Thank you,

    Dario

    xcoldnet Guest

  8. #7

    Default Re: Avoid Duplicate Inserts

    Hi Larry,

    Thanks for your input on this. Yes, I confirm that this is a simple insert. I
    know it is not necessary to have a SP for a single insert. But the way I?m
    doing is, all database operations are done through SP?s.

    Something wrong in doing that? But even when I insert through CF, without SPs,
    it inserts two records. Please advice.

    Thanks / Manu.

    wmanu Guest

  9. #8

    Default Re: Avoid Duplicate Inserts

    No, there is nothing wrong with using SP's for all your transactions... It's
    quite a good idea, to centralize your queries. I would go for grouping them in
    CFC's but so many people so many opinions, right? :)
    But could you show the code so we can take a look at it, maybe there is
    something wrong with the insert statement.. (I doubt it, but you never know)


    pukkel Guest

  10. #9

    Default Re: Avoid Duplicate Inserts

    Hi All,

    Please find the code attached. I tried the same code from official PC and it
    inserting only one record but if I do this from my laptop (slower) it inserting
    two records. Any help is greatly appreciated.

    Many thanks for your time.


    User Input Form

    <TABLE CELLPADDING="#SUBCELLPADDING#" CELLSPACING="#SUBCELLSPACING#"
    BGCOLOR="#SUBBGCOLOR#">
    <FORM NAME="AddProduct" METHOD="post">

    <TR BGCOLOR="FFFFFF"><TD>Product Name</TD>
    <TD><INPUT TYPE="Text" NAME="Name" SIZE="30" MAXLENGTH="30" <CFIF
    IsDefined('Name')>VALUE="#Name#"</CFIF>></TD></TR>

    <TR BGCOLOR="FFFFFF"><TD>Price Full</TD>
    <TD><INPUT TYPE="Text" NAME="PriceFull" SIZE="10" MAXLENGTH="10" <CFIF
    IsDefined('PriceFull')>VALUE="#PriceFull#"</CFIF>></TD></TR>

    <TR BGCOLOR="FFFFFF"><TD>Price Loose</TD>
    <TD><INPUT TYPE="Text" NAME="PriceLoose" SIZE="10" MAXLENGTH="10" <CFIF
    IsDefined('PriceLoose')>VALUE="#PriceLoose#"</CFIF>></TD></TR>

    <TR BGCOLOR="FFFFFF"><TD>Stock Full</TD>
    <TD><INPUT TYPE="Text" NAME="StockFull" SIZE="10" MAXLENGTH="10" <CFIF
    IsDefined('StockFull')>VALUE="#StockFull#"</CFIF>></TD></TR>

    <TR BGCOLOR="FFFFFF"><TD>Stock Loose</TD>
    <TD><INPUT TYPE="Text" NAME="StockLoose" SIZE="10" MAXLENGTH="10" <CFIF
    IsDefined('StockLoose')>VALUE="#StockLoose#"</CFIF>></TD></TR>

    <TR BGCOLOR="FFFFFF"><TD>Sales Tax</TD>
    <TD><INPUT TYPE="Checkbox" NAME="Salestax" <CFIF IsDefined('Salestax') AND
    Salestax EQ 1>CHECKED</CFIF> <CFIF NOT IsDefined('Salestax')>CHECKED</CFIF>>
    <FONT COLOR="GRAY">(#SalesTaxResultSet.ItemValue#%)</FONT></TD></TR>

    <TR BGCOLOR="FFFFFF"><TD>Aditional Sales Tax</TD>
    <TD><INPUT TYPE="Checkbox" NAME="AditionalSalestax" <CFIF
    IsDefined('AditionalSalestax') AND AditionalSalestax EQ 1>CHECKED</CFIF>> <FONT
    COLOR="GRAY">(#AditionalSalestaxResultSet.ItemValu e#%)</FONT></TD></TR>

    <TR BGCOLOR="FFFFFF"><TD>Excise</TD>
    <TD><INPUT TYPE="Checkbox" NAME="Excise" <CFIF IsDefined('Excise') AND Excise
    EQ 1>CHECKED</CFIF>> <FONT
    COLOR="GRAY">(#ExciseResultSet.ItemValue#%)</FONT></TD></TR>

    <TR BGCOLOR="FFFFFF"><TD COLSPAN="2" ALIGN="right"><INPUT TYPE="Submit"
    NAME="DoSubmit" VALUE="Add Product" ONCLICK="return Validate();"></TD></TR>

    <INPUT TYPE="Hidden" NAME="CategoryID" VALUE="#IDX#">
    </FORM>
    </TABLE>

    Calling SP for Insert

    <CFTRY>
    <CFSTOREDPROC PROCEDURE="PutProduct" DATASOURCE="#DSNNAME#">
    <CFPROCPARAM VALUE="#Name#" DBVARNAME="@strName"
    CFSQLTYPE="CF_SQL_VARCHAR">
    <CFPROCPARAM VALUE="#PriceFull#" DBVARNAME="@decPriceFull"
    CFSQLTYPE="CF_SQL_MONEY">
    <CFPROCPARAM VALUE="#PriceLoose#" DBVARNAME="@decPriceLoose"
    CFSQLTYPE="CF_SQL_MONEY">
    <CFPROCPARAM VALUE="#StockFull#" DBVARNAME="@decStockFull"
    CFSQLTYPE="CF_SQL_MONEY">
    <CFPROCPARAM VALUE="#StockLoose#" DBVARNAME="@decStockLoose"
    CFSQLTYPE="CF_SQL_MONEY">
    <CFPROCPARAM VALUE="#CategoryID#" DBVARNAME="@intCategoryID"
    CFSQLTYPE="CF_SQL_INTEGER">
    <CFPROCPARAM VALUE="#SalesTax#" DBVARNAME="@intSalesTax"
    CFSQLTYPE="CF_SQL_BIT">
    <CFPROCPARAM VALUE="#Excise#" DBVARNAME="@intExcise" CFSQLTYPE="CF_SQL_BIT">
    <CFPROCPARAM VALUE="#Vat#" DBVARNAME="@intVat" CFSQLTYPE="CF_SQL_BIT">
    <CFPROCPARAM VALUE="#AditionalSalesTax#" DBVARNAME="@intAditionalSalesTax"
    CFSQLTYPE="CF_SQL_BIT">
    </CFSTOREDPROC>
    <CFSET STATUS = 1>

    <CFCATCH>
    <CFSET STATUS = 0>
    </CFCATCH>

    </CFTRY>


    Stored Procedure for doing INSERT

    CREATE Proc PutProduct
    @strName Varchar(40),
    @decPriceFull Float(8),
    @decPriceLoose Float(8),
    @decStockFull Float(8),
    @decStockLoose Float(8),
    @intCategoryID Int,
    @intSalesTax Int,
    @intAditionalSalesTax Int,
    @intExcise Int,
    @intVat Int

    As

    Begin
    INSERT INTO Products ( Name,
    CategoryID,
    PriceLoose,
    SalesTax,
    AditionalSalesTax,
    Excise,
    Vat,
    PriceFull,
    Created,
    Updated,
    Status,
    UserID
    )
    VALUES ( @strName,
    @intCategoryID,
    @decPriceLoose,
    @intSalesTax,
    @intAditionalSalesTax,
    @intExcise,
    @intVat,
    @decPriceFull,
    GetDate(),
    GetDate(),
    1,
    1
    )

    DECLARE @ProductIDX Int
    SET @ProductIDX = SCOPE_IDENTITY()

    INSERT INTO ProductStock ( IDX,
    StockFull,
    StockLoose
    )
    VALUES ( @ProductIDX,
    @decStockFull,
    @decStockLoose
    )
    End


    GO

    wmanu Guest

  11. #10

    Default Re: Avoid Duplicate Inserts

    On the laptop you're using the same browser? There are no javascript errors of
    some sort? (what does that return Validate() do?)
    And, which record gets inserted twice, the product or the productstock?

    pukkel Guest

  12. #11

    Default Re: Avoid Duplicate Inserts

    Hi,

    Thanks again for your input on this.

    I?m using the same settings for my PC and Laptop with Windows XP & IE6. Even
    the software installed is the same. The only difference is the Hardware
    configuration.

    There is no JavaScript error on any of the page, function Validate() is just
    checking whether all the fields have been entered or not. If all entered then
    submit the form or return an error message.

    Both Products and Stock are getting inserted twice through Laptop, even the
    seconds are the same.

    Many thanks for your time.

    Regards / Manu.

    wmanu Guest

  13. #12

    Default Re: Avoid Duplicate Inserts

    Are you really sure the form is not submitted twice?
    And does the same behaviour take place when you use EXEC inside a cfquery tag instead of the cfstoredproc? Maybe it's some form of caching..
    pukkel Guest

  14. #13

    Default Re: Avoid Duplicate Inserts

    Hi,

    Your input on this is much appreciated. As far as I know, I?m clicking only
    once. If you EXEC the SP, it inserts only one record. The problem occurs when
    we try to insert through a form from a low performance system.

    I?m using <META HTTP-EQUIV="Pragma" CONTENT="no-cache">on every page to avoid
    caching.

    I experienced this problem many times in different occasions. Is this
    incurable disease?

    Regards / Manu.

    wmanu Guest

  15. #14

    Default Re: Avoid Duplicate Inserts

    Hmm I never seen anything like this happening... My clues are running out.
    Maybe this could shed some light on things:
    [url]http://coldfusion.sys-con.com/read/41757.htm[/url] (scroll down to "A Bug")..
    That article states (in short) that if there are problems with variables sent
    to a stored procedure (in this case through cfprocparam) that could lead to an
    error, which in turn could lead to a second insert without rolling back the
    first insert.
    And I see one thing in regard to this with your cfstoredproc call: the
    cfstoredproc tag does not send the variables in the order which SQLServer
    expects them.
    But then again, this all doesn't cover the situation where the query only
    flaws on a low performance system.
    To that question I think I don't have an answer..

    pukkel Guest

  16. #15

    Default Re: Avoid Duplicate Inserts

    I have sorted this problem on my site in this way:

    <cftransaction>

    if record is already present... do nothing, or display a message

    else.. insert the record

    </cftransaction>

    In this case if there is a double submission for any reason, it is only
    inserted once.

    IHTH,

    Dario

    xcoldnet Guest

  17. #16

    Default Re: Avoid Duplicate Inserts

    I agree with xcoldnet. I always use some type of if exists (.... ) logic and if the record exists I don't insert it. It avoids having to worry about double form submissions.
    mxstu Guest

  18. #17

    Default Re: Avoid Duplicate Inserts

    Hi All,

    Sorry for the delay in response. I rectified the problem by implementing Dario?s solution, with IF EXISTS (?). Thanks a million for all your time and help.

    Many thanks / Manu.
    wmanu Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139