我将返回一个结果数组,并将其放入foreach循环中,并且每个结果都检查一个量。只要满足条件,就应该插入行。但是,没有数据插入数据库,但是错误日志也为空,即:我没有错误。
foreach ($invoices as $invoice) {
if ($invoice->amount <= $this->amount) {
$stmt = $db->prepare("INSERT INTO `payment_link` (`amount`) VALUES (:amount)");
$stmt->bindValue(":amount", $invoice->amount, PDO::PARAM_STR);
$stmt->execute();
}
}
print_r($invoices)
给我:
Array ( [0] => App\Models\Invoice Object ( [customer_id] => [description] => [date_created] => [amount] => 300.00 [total:App\Models\Invoice:private] => [invoice_id] => [line_description] => [line_amount] => [errors] => Array ( ) ) [1] => App\Models\Invoice Object ( [customer_id] => [description] => [date_created] => [amount] => 500.00 [total:App\Models\Invoice:private] => [invoice_id] => [line_description] => [line_amount] => [errors] => Array ( ) ) ) Array ( [0] => App\Models\Invoice Object ( [customer_id] => [description] => [date_created] => [amount] => 300.00 [total:App\Models\Invoice:private] => [invoice_id] => [line_description] => [line_amount] => [errors] => Array ( ) ) [1] => App\Models\Invoice Object ( [customer_id] => [description] => [date_created] => [amount] => 500.00 [total:App\Models\Invoice:private] => [invoice_id] => [line_description] => [line_amount] => [errors] => Array ( ) ) )
答案 0 :(得分:1)
[基于问题中的代码,而非注释中的讨论]
您需要保持警惕;让计算机告诉您为什么它没有插入任何行。从php doc:globalState
成功返回 TRUE ,失败返回 FALSE 。。
测试结果并将其发布到错误日志(或回显到屏幕,任何适合您的方法)。
$stmt->execute();
如果没有 反馈,则表明循环未执行或前面的语句之一失败。 (如果成功,它们还返回TRUE,如果失败,则返回FALSE)。
如果显示“插入失败”,则可以从PDOStatement::errorinfo获取更多详细信息。
如果显示“行插入成功”,那么还有一些要调查的内容。 (例如:程序是否使用TRANSACTIONS并需要提交?)
从收集您没有的信息开始,它将帮助您找到实际的问题。