Skip to main content
Inspiring
Feb 16, 2013 3:02 PM UTC
Question

insert and update a record on the same form

I would like to use same form to insert and update a record to database.

I use Dreamweaver to have a form.

I tried many ways, it seems that I need have insert and update on the different form.

In principle, I should be able to check record exists or not, if yes then update the record, if not then insert a new record.

Can you please help and advise is it can be done using ColdFusion?

If yes, can you please advise where I can get example for this?

Your help and information is great appreciated,

Regards,

Iccsi

    This topic is closed to new replies. Start a new post to keep the conversation going.

    9 replies

    WolfShade
    Legend
    Feb 18, 2013 2:03 PM UTC

    Every database record should have a unique ID.  Doesn't matter if it's plain integers, alphanumeric, or a generated UID.

    If you give the form a field for the ID of the record and default it to 0, this will be beneficial.  On the query, set it to check for the value of the ID - if it's 0, then write code to INSERT; if it's anything else, write code to UPDATE.

    ^_^

    Translate
    iccsiAuthor
    Inspiring
    Feb 18, 2013 3:31 PM UTC

    Yes, thanks for the message and help,

    Web form has POST and GET method.

    I can use POST method form and running a stored procedure to check primary key in the table and doing insert or update in the stored procedure.

    Please let me know if I am wrong,

    Thanks again for the message and help,

    Regards,

    Iccsi,

    Translate
    Inspiring
    Feb 16, 2013 3:16 PM UTC

    The part where you check for existing records is done by querying the database.  This could be done as part of a stored procedure that you call with ColdFusion or as a cfquery.  I use Dreamweaver in code view to write my ColdFusion code.

    Translate
    iccsiAuthor
    Inspiring
    Feb 16, 2013 3:37 PM UTC

    Thanks a million for the message and helping,

    Is it possible  to have link for sample code?

    Thanks again,

    Regards,

    Iccsi,

    Translate
    Inspiring
    Feb 17, 2013 10:44 PM UTC

    Hello, Iccsi.

    You could try something like this.

    On your processing page / section try something like this:

    Check for existence of database record.

    <cfquery name="rsCheck" datasource="datasource">

    . . . query content here . . .

    </cfquery>

    Evaluate and process based on query

    <cfswitch expression="#rsCheck.RecordCount#">

    <cfcase value="0">

    No record found - Insert process here

    Redirect to a custom insert page/section message

    <cflocation url="redirect-to-desired-location.cfm?RecordProcess=Insert" addtoken="yes|no">

    </cfcase>

    <cfcase value="1">

    Record found - Update process here

    Redirect to a custom update page/section message

    <cflocation url="redirect-to-desired-location.cfm?RecordProcess=Update" addtoken="yes|no">

    </cfcase>

    </cfswitch>

    Of course you can tweak / change the above to fit your needs. This is just an idea to try.

    Leonard B

    Translate