php - INSERT 不适用于 PDO 准备好的语句

标签 php mysql pdo

我正在升级我的网站以使用 PDO。大多数代码都运行良好,但我有一个 INSERT 语句无法开始工作。它适用于我的本地服务器,但不适用于实时站点。我检查了表结构,它们是相同的。

这是我的代码

try {

    foreach ($unique_product_line_ids AS $key => $value) {

        $query_insertSQL = "
            INSERT INTO tblCarts (
            date_created
            , session_id
            , product_line_id
            , posted_by_id
            , quantity
            ) VALUES (
              :date_created
            , :session_id
            , :product_line_id
            , :posted_by_id
            , :quantity1
            )
            ON DUPLICATE KEY UPDATE quantity = :quantity2
                    ";
        $insertSQL = $conn->prepare($query_insertSQL);

        $insertSQL->execute(array(
            ':date_created'=>$date_created
            , ':session_id'=>$session_id
            , ':product_line_id'=>$key
            , ':posted_by_id'=>$_SESSION['member']['individual_id']
            , ':quantity1'=>$value
            , ':quantity2'=>$value
        ));

        $rc_insertSQL = $insertSQL->rowCount();

        // close connection
        $insertSQL->closeCursor();

    }

} catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
}

我已经检查了我的 unique_product_line_ids 数组中的值,它们存在正常。我也尝试删除 ON DUPLICATE KEY UPDATE 行(及其相应的参数),但这没有任何区别。

最佳答案

准备语句的优点之一是您准备一次并执行多次,所以 我会像这样重写你的代码:

try {

    $totalInserts = 0;
    
    $query_insertSQL = 
    "
        INSERT INTO tblCarts (
              date_created
            , session_id
            , product_line_id
            , posted_by_id
            , quantity
        ) 
        VALUES (
              :date_created
            , :session_id
            , :product_line_id
            , :posted_by_id
            , :quantity
        )
        ON DUPLICATE KEY UPDATE 
            quantity = :quantity
    ";
    
    $insertSQL = $conn->prepare($query_insertSQL);
    
    
    foreach ($unique_product_line_ids AS $key => $value) 
    {
        $insertSQL->execute(array(
              ':date_created'   => $date_created
            , ':session_id'     => $session_id
            , ':product_line_id'=> $key
            , ':posted_by_id'   => $_SESSION['member']['individual_id']
            , ':quantity'       => $value
        ));

        $totalInserts += $insertSQL->rowCount();

        /**
         * to debug if any error
         */
        var_export($conn->errorInfo());
        
        // close connection - does not need anymore while you don't 
        // prepare it multiple times
        //$insertSQL->closeCursor();
        
    }

} 
catch(PDOException $e) 
{
    echo 'ERROR: ' . $e->getMessage();
}

注意事项:

您可以根据需要多次使用一个变量, 所以不需要使用 :quantity1:quantity2 直到它们不同。 :quantity 就够了。

要在出现错误时进行调试,您可以使用 $conn->errorInfo();(连接级别)或 $insertSQL->errorInfo();(查询水平)

关于php - INSERT 不适用于 PDO 准备好的语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25523978/

相关文章:

php - 如何将 Javascript 发布到 MySQL 数据库

mysql - SQL 设置日期戳

mysql - 查找列值至少出现两次的行?

php - PHP 与 MYSQL 的连接繁忙?

PHP 数据库中存储的变量值不正确

php - 保护数据安全的最佳方式 PHP + MYSQL

php - Facebook API 内的分页(照片)

php - 使用 MySQL 中的临时表

php - 修改 SQL 以包含额外的表

php - 我可以在 PHP 中混合 MySQL API 吗?