下面的代码片段是在我的php文件中,它作为REST接口。
(...)
if ($stmt = $connection->prepare("INSERT INTO Resource VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)")){
$stmt->bind_param("sssssssii", $email, $timestamp, $title, $desc, $lat, $lng, $alt, $full, $cat);
// 1)
if($stmt->execute()){
// insertion of Resource successful
// 2)
}else{
// insertion of Resource failed
echo "INSERTION_RESOURCE_FAILED";
$stmt->close();
die;
}
(...)
之前的版本执行正确,没有两个额外的整数字段作为参数。我通过在其中放置echo来"调试"php文件。在位置1)我收到了回音,在位置2)没有。这对我来说意味着stmt->execute()失败了。但如果失败了,为什么我也没有收到else块的回显呢?我怎样才能更好地调试这些问题?注意,我检查了POST值,它们是正确的。
我还声明了
error_reporting(E_ALL);
紧接<?php
,
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
在每组查询之后。但是,没有错误回显。有什么问题吗?~
就像Linus提到的,它是缺失的display_errors()
。所以最后我发现这是外键的问题。但是多亏了你的回答,我终于能够调试php了!
好的,所以错误实际上是缺失的display_errors()
。只有在声明它之后,脚本才返回错误的堆栈。在我的例子中,这是一个与sql相关的问题。
下面是脚本的开头:
<?php
ini_set("display_errors", 1);
ini_set("track_errors", 1);
ini_set("html_errors", 1);
error_reporting(E_ALL);
// (...)