这让我抓狂了一整天 - 现在我需要一些帮助......
我尝试了两种将数据插入数据库的方法,但都以我以前从未见过的方式失败了!
仅使用 mysqli->query("...")
- 导致在实际查询末尾出现尾随逗号,如 mysqld.general.log 中所示。有多少个字段,就有多少个额外的逗号。
使用 mysqli->prepare
方法会导致实际的 prepare 语句出现在日志中!
我在下面包含我的代码以及我在日志中看到的内容..
全局 $mysqli 对象首先在包含文件中创建 - 这肯定有效,因为 select 语句工作正常...
非常感谢任何帮助。
代码:-
function addplaces($placeID,$placename,$easting,$northing,$latitude,$longitude,$parish,$inuse,$shire)
{
global $mysqli;
$mysqli->query("INSERT INTO `places` (`placeID`,`placename`,`easting`,`northing`,`latitude`,`longitude`,`parish`,`inuse`,`shire`) VALUES ($placeID,$placename,$easting,$northing,$latitude,$longitude,$parish,$inuse,$shire)");
}
日志输出:-
9594 Query INSERT INTO
places
(placeID
,placename
,easting
,northing
,latitude
,longitude
,parish
,inuse
,shire
) VALUES ("12672","Accrington","375500","428500","53.752300262451","-2.3730099201202","Accrington","1",Lancashire,,,,,,,,) 9594 Quit
代码:-
function addplaces($placeID,$placename,$easting,$northing,$latitude,$longitude,$parish,$inuse,$shire)
{
global $mysqli;
$stmt = $mysqli->prepare("INSERT INTO places (placeID,placename,easting,northing,latitude,longitude,parish,inuse,shire) VALUES (?,?,?,?,?,?,?,?,?)") or die ("Could not Prepare Statement");
$stmt->bind_param('sssssssss', $placeID,$placename,$easting,$northing,$latitude,$longitude,$parish,$inuse,$shire );
$stmt->execute();
}
日志输出:-
9595 Prepare INSERT INTO places ( placeID, placename, easting, northing, latitude, longitude, parish, inuse, shire ) VALUES (?,?,?,?,?,?,?,?,?) 9595 Close stmt 9595 Quit
我已经尝试用单引号和双引号将值字段括起来,但仍然得到相同的结果...
此外,由于这是我的第一篇文章,我不确定我是否掌握了这个编辑器的窍门,所以如果我说错了请原谅我..
最佳答案
在我看来,您的 $shire 变量没有引号。
关于PHP Mysqli - INSERT 在实际查询中导致尾随逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18816440/