撇号导致插入问题


Apostrophe causing problems with insert

嗨,我

正在使用php将一些数据插入MS Access数据库,在大多数情况下工作正常,唯一一次不起作用,据我所知,这是字段中有'的地方,在这种情况下它是一个地址,即圣约翰路。

这是我正在使用的查询语句:

$sql = "insert into tempaddress (`id`, `StreetAddress`, `Place`, `PostCode`) values ('".$item["Id"]."', '".$item["StreetAddress"]."', '".$item["Place"]."','$SearchTerm')";  CustomQuery($sql);

这就是我得到的错误 http://prntscr.com/58jncv

我很确定它只能是字符串文本中的'弄乱了它,我该如何更改?

撇号会断开 SQL 字符串。因此,您应该手动在SQL字符串中的每个撇号之前添加斜杠,或使用PHP内置的函数addslashes()

例:

$sql = "INSERT INTO myTable (value) VALUES ('Text that shouldn't break')";
$sql = addslashes($sql); // outputs "INSERT INTO myTable (value) VALUES ('Text that shouldn'''t break')"

来源 : php.net/manual/en/function.addslashes.php

谢谢,最后我选择了str_replace("'", ", $string);

您将 '

' 引号与 php 变量 $SearchTerm 一起使用,并在列名前使用反斜杠。

将查询语句更改为:

$sql = "insert into tempaddress ('`id'`, '`StreetAddress'`, '`Place'`, '`PostCode`) values ('".$item["Id"]."', '".$item["StreetAddress"]."', '".$item["Place"]."',$SearchTerm)";  CustomQuery($sql);