Insert Record With cfloop...

Ask a Question related to Coldfusion - Getting Started, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. CFLOOP not working for record insert as expected
      Have you tried <cfdumping the queries to make sure they contain what you expect?
    3. insert record loop
      Can anyone help me to create an insert record loop using asp vbs?
    4. 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...
    5. 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... ...
  3. #2

    Default 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

  4. #3

    Default 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

  5. #4

    Default 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

  6. #5

    Default 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

  7. #6

    Default 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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139