使用 LIKE 语句构建 Coldfusion 查询问题
我正在尝试用这一行构建动态 sql 语句
<cfset SQL = "SELECT url, MONTH(event_date) AS months, YEAR(event_date) AS year, event_date, title from events where title LIKE '%#form.event_name#%' ">
<cfquery name="results" >
#SQL#
</cfquery>
似乎 like 子句有问题。有什么想法吗?我需要转义%吗?
谢谢
I am trying to build a dynamic sql statement with this line
<cfset SQL = "SELECT url, MONTH(event_date) AS months, YEAR(event_date) AS year, event_date, title from events where title LIKE '%#form.event_name#%' ">
<cfquery name="results" >
#SQL#
</cfquery>
Seems there is a problem with the like clause. Any ideas? Do I need to escape the %?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 CFQUERY 中,ColdFusion 将自动用双引号替换
#SQL#
中的单引号。因此,从理论上讲,您必须像这样编写查询:
但是...接受表单变量并直接在查询中使用它而不进行进一步验证是非常危险的。对我来说这似乎是 SQL 注入攻击的邀请。
我宁愿使用
像这样:Within a CFQUERY, ColdFusion will replace single quotes in
#SQL#
with double quotes automagically.So in theory you would have to write your query like this:
BUT... It's very dangerous to accept a form variable and use it without further validation directly in your query. Seems like an invitation for SQL injection attacks to me.
I'd rather use
<cfqueryparam>
like so: