Skip to main content
Inspiring
February 16, 2013
Question

insert and update a record on the same form

  • February 16, 2013
  • 2 replies
  • 4652 views

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 has been closed for replies.

    2 replies

    WolfShade
    Legend
    February 18, 2013

    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.

    ^_^

    iccsiAuthor
    Inspiring
    February 18, 2013

    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,

    Inspiring
    February 16, 2013

    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.

    iccsiAuthor
    Inspiring
    February 16, 2013

    Thanks a million for the message and helping,

    Is it possible  to have link for sample code?

    Thanks again,

    Regards,

    Iccsi,

    Inspiring
    February 17, 2013

    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