【问题标题】:PDO SQL won't add new entriesPDO SQL 不会添加新条目
【发布时间】:2015-03-24 19:17:24
【问题描述】:

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

$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();

【问题讨论】:

  • 您没有检查对PDO 的调用是否有错误,因此如果有任何错误,您将看不到它们。

标签: php sql pdo insert


【解决方案1】:

“没有显示错误...”

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

即:product-id,MySQL 将其翻译为“产品 minus id”等。

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

将所有包含连字符的列都用勾号括起来。

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

等等

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

注意:

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

【讨论】:

  • 或者你可以总是使用记号;-)
  • 嗯....,“打勾还是不打勾”?如果威廉是一名程序员 Sam,他会怎么做? @JayBlanchard
  • 我想我什至无法猜到@Fred -ii- Mornin' Ralph!
  • 我认为他会打勾,如果他有一个 dawg; 早安山姆! - @JayBlanchard
【解决方案2】:

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

像这样修改你的代码。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-04
    • 1970-01-01
    • 2021-06-15
    • 2016-10-03
    • 2011-09-01
    • 2019-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多