Variable in database query?

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default Variable in database query?

    Here is a function I am attempting to use in a web application I'm
    developining. Basically, I am attempting to create a function to generate a
    <select> variable in a form. I want the select variable's default option to be
    based upon the value that's already in the database. So, for instance, if the
    selectbox is for value F_E1, and the value in the database is "YES", then I
    want "yes" to be pre-selected in the form. What I wanted to do was pass a
    string that said something like "F_E1" to the function, which would then
    generate a form entry based on the database value of query.F_E1.

    The error I'm getting is as follows: The variable RubaReports. ends with a "."
    character. You must supply an additional structure key or delete the "."
    character.

    Any ideas?

    <cffunction name="gen_select">
    <cfargument name="field_name" type="string" required="yes">
    <cfoutput query="RubaReports">
    <cfif RubaReports.#field_name# IS "N/A" OR RubaReports.#field_name# IS "">
    <select name="#field_name#">
    <option value="Yes">Yes</option>
    <option value="No">No</option>
    <option value="N/A" selected>N/A</option>
    </select>
    <cfelse>
    <select name="#field_name#">
    <option value="Yes"<cfif RubaReports.#field_name# IS "Yes">
    selected</cfif>>Yes</option>
    <option value="No"<cfif RubaReports.#field_name# IS "No">
    selected</cfif>>No</option>
    <option value="N/A">N/A</option>
    </select>
    </cfif>
    </cfoutput>
    </cffunction>

    walkeraj00 Guest

  2. Similar Questions and Discussions

    1. PARSING A QUERY OF QUERY WITH A VARIABLE VALUE
      On my first page the user selects a project. I am using the variable SelectedProject: <cfset SelectedProject =...
    2. Using a Variable to create the SQL Query
      I need to create a "dynamic" Update query. I want to store the meet of the command in a variable and then reference the variable in in query. ...
    3. Using variable in sql query
      Hello again, I'm trying to run an sql query using a string as in the following: <cfset string = "address1=' #evaluate("form.param#count#")# ',...
    4. Help with variable query?
      I'm having an arbitrary query input by the user, then to make that query into an excel file it must be put into a table similar to below. Since I...
    5. Embedding a variable in an SQL Query
      I am getting the following error when running : $result = mysql_query("SELECT * FROM users WHERE Username = $ThisUsername"); $num_rows =...
  3. #2

    Default Re: Variable in database query?

    First, you have to scope your field_name variable. The scope is arguments.

    I know you have to change RubaReports.#field_name# to something else, but I
    forget what. Perhaps, something like this.

    RubaReports["#field_name#"]

    or something else

    Dan Bracuk Guest

  4. #3

    Default Re: Variable in database query?

    Yes, of course! That is the correct syntax, I believe, but now I am getting
    the following error:

    Complex object types cannot be converted to simple values.
    The expression has requested a variable or an intermediate expression result
    as a simple value, however, the result cannot be converted to a simple value.
    Simple values are strings, numbers, boolean values, and date/time values.
    Queries, arrays, and COM objects are examples of complex values.

    The most likely cause of the error is that you are trying to use a complex
    value as a simple one. For example, you might be trying to use a query variable
    in a <CFIF> tag. This was possible in ColdFusion 2.0 but creates an error in
    later versions.

    The error occurred in /oradced/webpages/dca/ruba/report/Ruba_Detail.cfm: line
    19

    17 : <cfargument name="field_name" type="string" required="yes">
    18 : <cfoutput query="RubaReports">
    19 : <cfif RubaReports["#field_name#"] IS "N/A" OR
    RubaReports["#field_name#"] IS "">
    20 : <select name="#field_name#">
    21 : <option value="Yes">Yes</option>

    Well, that's exactly right! I'm trying to use a query inside of a <cfif> tag!
    What's the solution to this? An intermediate variable?


    walkeraj00 Guest

  5. #4

    Default Re: Variable in database query?

    Probably a row number.
    Dan Bracuk Guest

  6. #5

    Default Re: Variable in database query?

    Here's the working code. I eliminated the last error by simply making an
    intermediate variable. Dan, it was unnecessary to specifically scope the
    argument. I just wasn't using it right.

    <cffunction name="gen_select">
    <cfargument name="field_name" type="string" required="yes">
    <cfoutput query="RubaReports">
    <cfset fieldresult=RubaReports["#field_name#"]>
    <cfif fieldresult IS "N/A" OR fieldresult IS "">
    <select name="#field_name#">
    <option value="Yes">Yes</option>
    <option value="No">No</option>
    <option value="N/A" selected>N/A</option>
    </select>
    <cfelse>
    <select name="#field_name#">
    <option value="Yes"<cfif fieldresult IS "Yes"> selected</cfif>>Yes</option>
    <option value="No"<cfif fieldresult IS "No"> selected</cfif>>No</option>
    <option value="N/A">N/A</option>
    </select>
    </cfif>
    </cfoutput>
    </cffunction>

    walkeraj00 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