Avoiding save record while refresh after submit in coldfusion

Posted on 2007-10-11
Last Modified: 2013-12-24
In my form I have a drop down with list of program. Also I have an add optioon , so once the user click on it I will get the text field and a radio button  where the user would enter all the info in click on save submit. That will start the insert SP. So my question is once I do the submit I will get duplicate records in the database. How can I avoid duplicate submittion. Here is my CF code
 <cfparam name="form.hidden" default="0">
            <cfparam name="form.categoryname" default="">
            <cfif isDefined("form.btnsubmit3")>
            <cfloop index="variables.i" from="1" to="#ListLen(form.categoryname)#">
                       <cfset variables.thisCheckbox = ListGetAt(form.categoryname,variables.i)>
                   <cfstoredproc procedure="addCategory" datasource="HotBanana_Manager">
                            <cfprocparam type="in" cfsqltype="cf_sql_varchar" value="#variables.thisCheckbox#">        
                      <cfprocparam type="in" cfsqltype="cf_sql_varchar" value="#session.schoolcode#">
                              <cfprocparam type="in" cfsqltype="cf_sql_bit" value="#form.hidden#">
             <cfcatch type="database">
              There was a database error!
Question by:erikTsomik
    LVL 5

    Expert Comment

    Check condition using unique id before inserting a record whether it is already inserted or not.
    LVL 19

    Author Comment

    The id automatically generated so there is no way to check it.
    LVL 5

    Expert Comment

    In that form any other unique is present other than auto increment id. If no means you cannot avoid that problem. Because its not a language problem. You only set one unique name other than auto increment id.
    LVL 19

    Author Comment

    I can use category name
    LVL 5

    Expert Comment

    <cfquery name="exist" datasource="test">
    select * from example category_name = #category_name#
    <cfif exist.recordcount GT 0>
          Already exists
          Insert code here
    LVL 51

    Accepted Solution

    As I see it, there seem to be two issues

    1) Its sounds as if you're running a stored procedure when you shouldn't be.  If the code is behaving incorrectly, you need to change the conditions so it executes at the right time.  Otherwise, you're generating database activity when its not needed.  

    2) If duplicates are not allowed, your stored procedure should anticipate this and handle that condition.  In other words, only insert a record if one does not already exist.  

          INSERT INTO TableName (someColumn)
         SELECT  @someVariable ....
                        SELECT 1
                        FROM    SomeTable
                        WHERE  SomeColumn = @someValue

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
    Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
    The purpose of this video is to demonstrate how to make a WordPress Site faster and smaller in size by cleaning up the database. This will be demonstrated using a Windows 8 PC. Plugin WP Optimize will be used. Go to your WordPress login page. T…
    The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now