Ask a Question related to Coldfusion - Getting Started, Design and Development.
-
Thomas D #1
Insert Record With cfloop...
Can anyone tell me why "#herb_number#" is not evaluated as an existing form
field in the following code?
Thanks,
T
<cfset j = "1">
<cfloop index="HerbInsert" from="1" to="16">
<cfset herb_number = "Form.Herb#j#">
<cfset herb_number_dose = "Form.Herb#j#_dose">
<cfset herb_insert = "#herb_number#">
<cfset herb_insert_dose = "#herb_number_dose#">
<cfif isDefined("Form.herb_insert") and #Form.herb_insert# NEQ "">
<cfquery datasource="#application.DSN#">
INSERT INTO FormulaHerbs (formula_id, herb_id, herb_amount)
VALUES ('#Form.formula_id#', '#herb_insert#', '#insert_dose#')
</cfquery>
</cfif>
<cfset j=j+1>
</cfloop>
Thomas D Guest
-
Insert record
Hi all, I'm having a bit of a problem at the moment. I'm basically trying to insert a record to a MS Access database. I'm using dreamweaver MX. What... -
CFLOOP not working for record insert as expected
Have you tried <cfdumping the queries to make sure they contain what you expect? -
insert record loop
Can anyone help me to create an insert record loop using asp vbs? -
How to insert a new record
I am having problems with inserting a new record into access database using the detailsview control, the autonumber of the control does not update... -
insert a record
Hi every one, I have a problem inserting a record... error 500.100 (Operation must use an updateable query) or Unknown variable or something... ... -
Dan Bracuk #2
Re: Insert Record With cfloop...
Probably because this
<cfset herb_number = "Form.Herb#j#">
interprets the Form.Herb part as a string.
Originally posted by: Thomas D
Can anyone tell me why "#herb_number#" is not evaluated as an existing form
field in the following code?
Thanks,
T
<cfset j = "1">
<cfloop index="HerbInsert" from="1" to="16">
<cfset herb_number = "Form.Herb#j#">
<cfset herb_number_dose = "Form.Herb#j#_dose">
<cfset herb_insert = "#herb_number#">
<cfset herb_insert_dose = "#herb_number_dose#">
<cfif isDefined("Form.herb_insert") and #Form.herb_insert# NEQ "">
<cfquery datasource="#application.DSN#">
INSERT INTO FormulaHerbs (formula_id, herb_id, herb_amount)
VALUES ('#Form.formula_id#', '#herb_insert#', '#insert_dose#')
</cfquery>
</cfif>
<cfset j=j+1>
</cfloop>
I kind of understand that I can't get a variable evaluated from within a
variable, so is there another way to do this?
Error:
[Macromedia][SQLServer JDBC Driver][SQLServer]Syntax error converting the
varchar value 'Form.Herb1' to a column of data type int.
Dan Bracuk Guest
-
SteveBryant #3
Re: Insert Record With cfloop...
You should replace these lines:
<cfset herb_number = "Form.Herb#j#">
<cfset herb_number_dose = "Form.Herb#j#_dose">
with these lines:
<cfset herb_number = Form["Herb#j#"]>
<cfset herb_number_dose = Form["Herb#j#_dose"]>
Your code wasn't evaluating a form variable, but rather returning the name of
the variable that you wanted as the value (so the value would be "Form.Herb1"
instead of the value of the Herb1 form variable.
The code that I have gets the Herb1 key of the form structure (form variables
can be accessed as structures in CFMX and above).
HTH!
Steve
SteveBryant Guest
-
reenaroy #4
Re: Insert Record With cfloop...
replace these lines:
<cfset herb_number = "Form.Herb#j#">
<cfset herb_number_dose = "Form.Herb#j#_dose">
with
<cfset herb_number = "Form.Herb#j#">
<cfset herb_number_dose = evaluate(Form.Herb#j#_dose)>
reenaroy Guest
-
Stressed_Simon #5
Re: Insert Record With cfloop...
Don't use evaluate() use array notation:-
<cfset herb_number = Form["Herb#j#"]>
<cfset herb_number_dose = Form["Herb#j#_dose"]>
evaluate() has performance issues and in some cases can be a security risk.
99.9% there is a better way of doing things to avoid evaluate(), if you find
yourself in the 0.1% where you need to use it then you are probably something
very unorthodox.
Stressed_Simon Guest
-
Thomas D #6
Re: Insert Record With cfloop...
Thanks everyone for your wise words... Steve, your code worked flawlessly.
Knowing the Form variables are available in structures is cool...
Again, muchas gracias,
Thomas
Thomas D Guest



Reply With Quote

