Coldfusion发布到查询以构建查询

时间:2014-08-26 05:41:26

标签: mysql post coldfusion

美好的一天。

我想动态构建动态查询。 这就是我想做的事。

<cfform name="form1" method="post" action="mysql_fix.cfm">

<table width="450" border="1">
<tr>
    <td>Code</td>
    <td>
    <textarea name="codetoinsert" rows="5" cols="80"></textarea>
</td>
</tr>
<tr>
    <td></td>
    <td><cfinput type="submit" name="submit" value="submit">

    </td>
</tr>
<tr>
    <td></td>
    <td></td>
</tr>
<table>
</cfform>

依据我的结果

<cfset codeis = #replacenocase(FORM.codetoinsert,chr(34),#CHR(39)#,"All")#>
<cfset codeis = #replacenocase(codeis,"#CHR(39)##CHR(39)#",#CHR(39)#,"All")#>
<cfquery name="dodata" datasource="#datasrc#">
    #codeis#
</cfquery>

但是我得到了这个错误。错误在双引号中。

我发布在textarea

INSERT INTO payments
(description,amount,paytype,closed,customer,dateof,username,location,oldbalance,newbalance,invoiceno,closedtime,allocated,closeduser)
VALUES
('FOR Invoice No : 9960',0.00,'Account','Yes',7907,'2014/07/22 12:00:00','Ruby','2',3800.01,7600.02,9960,'2014/07/22 18:31:41','Yes','Ruby') 

这是错误。它放入''

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FOR Invoice No : 9960'',0.00,''Account'',''Yes'',7907,''2014/07/22 12:00:00'',''' at line 1 

1 个答案:

答案 0 :(得分:2)

我当然希望代码不会暴露给公众:它是灾难的秘诀。

但是,无论如何,当您遇到数据库问题时,首先要做的是查看发送给数据库的SQL。不是<cfquery>标记中的SQL(在您的情况下是<textarea>,而是CF实际发送到数据库驱动程序的SQL。它显示在调试输出中。

如果您检查了这一点,您很可能会发现CF正在逃避SQL语句中的所有单引号。这是设计(一个有问题的优点,IMO,但仍然)。

要使CF不这样做,请用preserveSingleQuotes()包装SQL语句。

附注:

  • 你不需要#周围的表达,除非他们在其中 字符串或以其他方式混淆为文字(&#34; When to use pound-signs&#34;)
  • 你不应该在SQL字符串中传递硬编码的数据值, 你应该将它们作为参数传递。它可能不是这样的 问题,如果这是为临时查询执行,但作为一般规则 (&#34; What one can and cannot do with <cfqueryparam>&#34)