2009-11-17 10 views
0

目前,我正在提交表单提交以解释其他垃圾单引号&。使用通用ODBC驱动程序的占位符

$form_field_value= str_replace("'", "''", stripslashes($form_field_value)); 

它来准备值使用插入:

$insert_sql = "insert into table (field) values ('".$form_field_value."')"; 
odbc_exec($conn, $insert_sql); 

从本质上讲,我想使用占位符,这些插入/更新语句。

我试图定义$par1$par2为文字,然后执行此

$insert_sql = "insert into table (field,txt) values (?,?)"; 
odbc_exec($conn, $insert_sql, $par1, $par2); 

它失败了,给了我这个错误:

警告:odbc_exec()[function.odbc-EXEC]:SQL错误:[Microsoft] [ODBC SQL Server驱动程序] COUNT字段不正确或语法错误,第10行中的SQLExecDirect中的SQL状态07001在test.php中

第10行是exec语句。


我无法找到使用此odbc驱动程序占位符的语法。有什么建议么?

连接变量$conn工作正常。


编辑:

一次尝试仍然失败 - odbc_execute()是一个未定义的功能。我必须使用odbc_exec()

$par1="eggs"; 
$par2="milk"; 

$insert_crs = "insert into table (field,txt) values (?,?)"; 
$stmt = odbc_prepare($conn, $insert_sql); 

odbc_exec($stmt, array($par1, $par2)); 

回答

1

http://www.php.net/manual/en/function.odbc-prepare.php,你应该准备然后执行你的SQL语句,你应该提供第三个参数一个新的数组dobc_execute():

 
<?php 
$a = 1; 
$b = 2; 
$c = 3; 
$stmt = odbc_prepare($conn, 'CALL myproc(?,?,?)'); 
$success = odbc_execute($stmt, array($a, $b, $c)); 
?> 

这意味着你的代码应该是这样的:

 
$insert_sql = "insert into table (field,txt) values (?,?)"; 
// the following line is new, compared to your code 
$stmt = odbc_prepare($conn, $insert_sql); 
// note that the following line wraps $par1 and $par2 with array() 
// struck out version was incorrect - copy/paste error :(

            
 
  
             odbc_exec($stmt, $insert_sql, array($par1, $par2));
            
  
odbc_execute($stmt, array($par1, $par2)); 
+0

感谢...是啊,我在浏览该网站,并结束见状odbc_execute而不是EXEC浏览....现在尝试它 – CheeseConQueso

+0

没有好处...我试图执行和执行...我认为这将是这种情况 – CheeseConQueso

+0

你可以发布你的改变的代码?你仍然得到完全相同的错误?而且,如果您复制/粘贴了我的代码,我自己做了一个错误 - 简单地纠正... – atk