PDO SQL不会添加新条目

时间:2015-03-24 19:17:25

标签: php sql pdo insert

没有显示错误,但由于某种原因,我在运行此脚本时无法添加新条目。任何帮助将不胜感激。

$sth = $dbh->prepare('INSERT INTO inventory(sku, product-id, product-id-type, price, minimum-seller-allowed-price, maximum-seller-allowed-price, item-condition, quantity, add-delete, will-ship-internationally, expedited-shipping, item-note, fulfillment-center-id)
VALUES(:sku, :product_id, :product_id_type, :price, :minimum, :maximum, :condition, :quantity, :add_delete, :international, :expedited, :note, :fulfillment)');
$sth->bindParam(':sku', $sku);
$sth->bindParam(':product_id', $product_id);
$sth->bindParam(':product_id_type', $product_id_type);
$sth->bindParam(':price', $price);
$sth->bindParam(':minimum', $minimum);
$sth->bindParam(':maximum', $maximum);
$sth->bindParam(':condition', $condition);
$sth->bindParam(':quantity', $quantity);
$sth->bindParam(':add_delete', $add_delete);
$sth->bindParam(':international', $international);
$sth->bindParam(':expedited', $expedited);
$sth->bindParam(':note', $note);
$sth->bindParam(':fulfillment', $fulfillment);
$sth->execute();

2 个答案:

答案 0 :(得分:3)

  

“没有显示错误......”

没有错误?因为,你没有检查它们。你的许多专栏都包含连字符,MySQL认为你想做数学。

I.e。:product-id,MySQL将其翻译为“product minus id”等。

setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION)
会发出语法错误信号。

用包含刻度线的连字符包裹所有列。

(sku, `product-id`, `product-id-type` ...

  • 在打开连接后立即添加$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

诺塔:

  • 您应该尽量避免使用连字符作为单词分隔符,而是使用下划线 这样你就不必使用刻度线了。

答案 1 :(得分:0)

这里添加了因为我无法在评论中正确编码。这不是问题的答案,但我怀疑会直接导致它。

像这样改变您的代码。

try { 
    // All $sth lines above in this section.

} catch (PDOException $e) {
    // this will print an exception out if there is one.
    echo $e->getMessage();
}