php - 尝试将数据插入 blob 时发生 PDO 异常

标签 php mysql exception pdo

我试图将一个非常大的 JSON 对象插入到 blob 中,但收到一个异常错误,指示无效参数号“未定义”。

代码:

<?php




        session_start();
        require_once('sconfig.php');
        require_once('mail/config.php');
        try{
                $token  = $_POST['stripeToken'];

                $customer = \Stripe\Customer::create(array(
                        'email' => $_SESSION['SESS_EMAIL'],
                        'card'  => $token
                ));
                $charge = \Stripe\Charge::create(array(
                        'customer' => $customer->id,
                        'amount' => $_SESSION['PLAN'],
                        'currency' => 'usd'
                ));

        } catch (Exception $e) {
                echo "<br>";
                echo "Handle your exception fool!";
        }
        //var_dump($customer);
        echo "<br>";
        //var_dump(json_decode($customer));
        echo "<br>";

        //echo $_SESSION['PLAN'];

        $pdo = new PDO(
                'mysql:host=' . DB_HOST . ';dbname=' . DB_DATABASE,
                DB_USER,
                DB_PASSWORD
        );
        //here we insert plan into the database following purchase
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
        $pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
        $session_var = $_SESSION['SESS_MEMBER_ID'];


        //ATTENTION!!!  All of these variable need to be changed when price gets changed
        if($_SESSION['PLAN'] === '3500'){
                $plan_var = 1;
                echo $plan_var;
                $sql = 'UPDATE accounting SET active = 1, plan = :plan_var WHERE id = :session_var';
                $sql2 = 'INSERT INTO transactions (customer_object, charge_object) VALUES(:customer, :charge)';
        }
        else if($_SESSION['PLAN'] === '2500'){
                $plan_var = 2;
                echo $plan_var;
                $sql = 'UPDATE accounting SET active = 1, plan = :plan_var WHERE id = :session_var';
                $sql2 = 'INSERT INTO transactions (customer_object, charge_object) VALUES(:customer, :charge)';
        }
        else if($_SESSION['PLAN'] === 'NULL'){
                echo "Call a Dr. Something bad happened, or the programmer needs to be fired";

                header("location: ../index.php?p=failed");
        }
        else {
                echo "This looks like a paid invoice. Thank you!";
                $plan_var = 9;
                echo '<br>';
                echo $plan_var;
                echo '<br>';
                echo '<pre>' . print_r($_SESSION, TRUE) . '</pre>';
                $sql = 'UPDATE accounting SET plan = :plan_var WHERE id = :session_var';
                $sql2 = 'INSERT INTO transactions (invoice_num) VALUES(:invoice_num)';
                //header("location: ../index.php?p=success");
        }

        $statement = $pdo->prepare($sql);
        $statement2 = $pdo->prepare($sql2);
        $statement->bindParam(':plan_var', $plan_var, PDO::PARAM_STR, 1);
        $statement->bindParam(':session_var', $session_var, PDO::PARAM_STR, 1);
        $statement2->bindParam(':customer', $customer, PDO::PARAM_LOB);
        $statement2->bindParam(':charge', $charge, PDO::PARAM_LOB);
        $statement2->bindParam(':invoice_num', $_SESSION['INVOICE_NUM'], PDO::PARAM_STR, 255);
        $user = $statement->execute();
        $user = $statement2->execute();

        var_dump($statement);

        //header("location: ../index.php?p=success");

        //echo $token;
?>

我收到的错误如下:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: parameter was not defined' in /usr/home/nyctelecomm/www/pages/scharge.php:79 Stack trace: #0 /usr/home/nyctelecomm/www/pages/scharge.php(79): PDOStatement->bindParam(':customer', Object(Stripe\Customer), 3) #1 {main} thrown in /usr/home/nyctelecomm/www/pages/scharge.php on line 79

如何使用 pdo 将 blob 数据存入 mysql 数据库?

最佳答案

基于您的堆栈跟踪

Fatal error: Uncaught exception 
'PDOException' with message 'SQLSTATE[HY093]: 
Invalid parameter number: parameter was not defined' in 

/usr/home/nyctelecomm/www/pages/scharge.php:79 Stack trace: 

#0 /usr/home/nyctelecomm/www/pages/scharge.php(79): 
        PDOStatement->bindParam(':customer', Object(Stripe\Customer), 3) 
#1 {main} thrown in /usr/home/nyctelecomm/www/pages/scharge.php on line 79

看起来 PHP 遇到了以下行 (#79)

$statement2->bindParam(':customer', $customer, PDO::PARAM_LOB);

我猜测您是否尝试将参数 :customer 绑定(bind)到未定义参数 :customer 的 SQL 语句中。查看 $sql2

的所有可能值
$sql2 = 'INSERT INTO transactions 
(customer_object, charge_object) VALUES(:customer, :charge)';

$sql2 = 'INSERT INTO transactions 
(customer_object, charge_object) VALUES(:customer, :charge)';

$sql2 = 'INSERT INTO transactions (invoice_num) VALUES(:invoice_num)';

您似乎并不总是绑定(bind) :customer 参数。

我会重构您的代码,以确保您没有绑定(bind) SQL 中不存在的参数。

关于php - 尝试将数据插入 blob 时发生 PDO 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29267833/

相关文章:

mysql - 查询删除字符串中最后一个逗号后的所有字符

mysql - 如何在 Mysql 中压缩连续的重复行?

ios - 如果 block 内发生异常会怎样?

c# - 异步方法 C# 中的异常处理

php - 如何使用PHP API分析字符串

php - 全文搜索不适用于某些表

c# - Oracle/SQL 命令仅显示条件发生更改的行

python - FileNotFoundError "try .. except IOError"未捕获时如何处理?

php - 在 PHP 中动态检查来自 MySQL 的动态复选框

php - 如何优化我的查询?