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.
Thanks a million for the message and helping,
Is it possible to have link for sample code?
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 . . .
Evaluate and process based on query
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">
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">
Of course you can tweak / change the above to fit your needs. This is just an idea to try.
Thanks for the message and help,
According to your code, it seems that I still need have one form for insert and one form for update.
I just use a form to process both and direct user to the right form.
please let me know, if I am wrong,
Thanks again for helping,
= = =
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.
= = =
I may be off track here, but reading this sentence in your original post, leads me to think there is a field(s) in your form that
you would be using to determine if the record exists or does not exist, i.e. product id, product name, etc.
When submitting the form, you would use the determinig field(s) to query the desired db table for record presence and based on
query results either update the respective record or add the information as a new record.
So --- one form, db table search, process according to db results
There are probably a couple of ways to do this, and I've done it in the past using various methods. One was to use variables as form values with null defaults, like this:
<cfparam name="user_email" default="" />
Then in the form:
<input name="email" id="email" type="text" value="#user_email#" />
This gives you a form you can use for inserts, as the values are blank. When you need to update a record, you usually pass something with a URL string, like this:
Then on the same page, between the <cfparam>s and the form, have this:
<cfquery name="myquery" datasource="mydsn">
Then have something where you assign each query item to the variabled you use in the form:
<cfset user_email = myquery.email>
When you load the page, and the URL variable is null, you get a blank form. When you pass a URL variable, the form is populated. The only thing left is to let the processing page know whether you are inserting or updating. I usually do this by changing the submit button.
<cfif URL.item eq "">
<input name="additem" type="submit" value="Add Record" />
<input name="updateitem" type="submit" value="Update Record" />
The on the processing page:
--- INSERT query here ---
--- UPDATE query here ---
The other way I've used is to use IsDefined() for your form element values, but I've had inconsistent results from IsDefined, so I avoid using it unless I have to.
We all have our own way of doing things. Personally, I try to avoid having both a form scope and a url scope on the same page. I'm very easily confused so I keep things as simple as possible.
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.
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,