cfquery param on query containing if statment
-
13-06-2021 - |
Question
How do you use cfqueryparam on this line of code?
SET mailing_list = <CFIF IsDefined("FORM.mailing_list")>#FORM.mailing_list#<CFELSE>0</CFIF>
Solution
you would just use two separate cfqueryparam's
SET mailing_list =
<cfif StructKeyExists(form,'mailing_lis't)>
<cfqueryparam cfsqltype="cf_sql_varchar" value "#FORM.mailing_list#" />
<cfelse>
0
</cfif>
OTHER TIPS
Like this:
SET mailing_list = <CFIF IsDefined("FORM.mailing_list")>
<cfqueryparam value="#FORM.mailing_list#" cfsqltype="cf_sql_integer" />
<CFELSE>
0
</CFIF>
On recent CFML engines, you can use the ternary conditional operator:
SET mailing_list = <cfqueryparam value="#IsDefined("FORM.mailing_list") ? FORM.mailing_list : 0#" cfsqltype="cf_sql_integer" />
(Though that is arguably less readable here.)
Best way is most likely to param it above your query and avoid any if altogether...
<cfparam name="Form.mailing_list" default="0" />
...
SET mailing_list = <cfqueryparam value="#FORM.mailing_list#" cfsqltype="cf_sql_integer" />
I generally use IIF
for short IF statements:
SET mailing_list = <cfqueryparam value="#iif(IsDefined("form.mailing_list"), form.mailing_list, 0)#" cfsqltype="cf_sql_integer" />
Otherwise I prefer the "long and clean" way:
<CFIF IsDefined("FORM.mailing_list")>
SET mailing_list = <cfqueryparam value="#FORM.mailing_list#" cfsqltype="cf_sql_integer" />
<CFELSE>
SET mailing_list = <cfqueryparam value="0" cfsqltype="cf_sql_integer" />
</CFIF>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow