I am looking for the best way to create a query based on the values submitted from a form in coldfusion.
I was going to create it using <cfif> statements but started running into issues of adding commas after an AND statement but not adding if its the last AND in the query. As well as making sure the first WHERE is included in the query.
See my example below:
<cfquery name="reportInfo" datasource="DataSource1">
SELECT *
FROM Basic_Info
<cfif company_name NEQ "false">
WHERE Sold_to_Party = '#company_name#',
<cfelse>
WHERE Sold_to_Party != '',
</cfif>
<cfif user_type EQ "both">
<cfelseif user_type EQ "rittal">
AND Sold_to_Party != 'Distributor Submission',
<cfelseif user_type EQ "distro">
AND Sold_to_Party = 'Distributor Submission',
</cfif>
<cfif status NEQ "false">
AND Status = '#status#'
<cfelse>
AND Status != ''
</cfif>
ORDER BY ID
</cfquery>
You can see I added elseif statements in so that I could add the same statement without a comma.
WHEREpart fine using what you have. - haxtbh