我正在尝试使用用户输入的表单数据来匹配我的trans_location列。我有一个下拉菜单,让用户可以选择多个位置。当他们选择多个时,它会在每个位置之间放置逗号。当我只选择一个位置时,结果会以正确的位置返回。但是当我选择多个位置时,它找不到任何位置。逗号是否只有一个名称,它不会搜索每个位置?
<cfset result = {} />
<cftry>
<cfset date1 = #CREATEODBCDATETIME(form.StartDate & '00:00:00')#>
<cfset date2 = #CREATEODBCDATETIME(form.EndDate & '23:59:59')#>
<cfquery datasource="#application.dsn#" name="GetLocationInfo">
SELECT *
FROM cl_checklists
WHERE date >= #date1# AND date <= #date2#
AND trans_location = '#form.Location#'
</cfquery>
<cfoutput>#date1#</cfoutput>
<cfoutput>#date2#</cfoutput>
<cfdump var="#GetLocationInfo#">
<cfcatch type="any">
<cfset result.error = CFCATCH.message >
<cfset result.detail = CFCATCH.detail >
</cfcatch>
</cftry>
我也尝试过这样的事情:
AND trans_location = <cfqueryparam value='#form.Location#' />
答案 0 :(得分:3)
您需要将IN
operator与cfqueryparam
一起使用list="true"
属性。 (这是cfqueryparam的快速帮助教程:https://www.petefreitag.com/item/677.cfm)
最后:始终总是,始终在向数据库发送参数时使用cfqueryparam
。
<cfset result = {} />
<cftry>
<cfset date1 = CREATEODBCDATETIME(form.StartDate & '00:00:00')>
<cfset date2 = CREATEODBCDATETIME(form.EndDate & '23:59:59')>
<cfquery datasource="#application.dsn#" name="GetLocationInfo">
SELECT *
FROM cl_checklists
WHERE date >= <cfqueryparam value="#date1#" cfsqltype="cf_sql_timestamp" />
AND date <= <cfqueryparam value="#date2#" cfsqltype="cf_sql_timestamp" />
AND trans_location IN ( <cfqueryparam value="#FORM.location#" cfsqltype="cf_sql_varchar" list="true" /> )
</cfquery>
<cfoutput>#date1#</cfoutput>
<cfoutput>#date2#</cfoutput>
<cfdump var="#GetLocationInfo#">
<cfcatch type="any">
<cfset result.error = CFCATCH.message >
<cfset result.detail = CFCATCH.detail >
</cfcatch>
</cftry>