ipn中的PDO不插入

时间:2014-01-31 08:18:23

标签: php pdo paypal paypal-ipn

嘿,我在ipn进程中在数据库中插入自定义数据时遇到问题,这就是场景,我使用http_build_query发送数据:

require_once 'classes/Crypt.php';
$crypt = new Crypt();
$crypt->Mode = Crypt::MODE_HEX;
$crypt->Key  = '!@#$%&*()_+?:';

$test = array('cmd'=>'_xclick',
                'business'=>'my_email',
                'notify_url'=> $home_url.'/ipn/ipn.php',
                'item_name'=>'name',
                'amount'=>'1.00',
                'currency_code'=>'USD',
                'lc'=>'US',
                'custom'=>$crypt->encrypt(serialize(array("username" => $username))));


                $url = "https://www.sandbox.paypal.com/cgi-bin/webscr?".http_build_query($test);
                header("Location:".$url);
                exit();

我的ipn-script,其中我使用PDO bindParam插入数据:

require_once '../classes/Crypt.php';

$crypt = new Crypt();
$crypt->Mode = Crypt::MODE_HEX;
$crypt->Key  = '!@#$%&*()_+?:';

$custom = unserialize($crypt->decrypt($_POST["custom"]));

$username = $custom['username'];

try
{
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $dbh->prepare("INSERT INTO products(product_name)
                    VALUES(:productName)");
    $stmt->bindParam(':productName', $productName);

    $productName = $username;

    $stmt->execute();
}
catch(PDOException $exception)
{
    $body .= "Fail: " . $exception->getMessage() . "\n";
}

没有插入,但我在日志中收到此错误:

Fail: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'product_name' cannot be null

数据发送肯定,我在指向paypal之前使用echo检查了它。 我的餐桌产品:

CREATE TABLE products (
    id int(11) NOT NULL auto_increment,
    product_name varchar(55) NOT NULL,

    PRIMARY KEY (id),
    UNIQUE KEY product_name (product_name)
);

目前我真的不知道为什么查询没有被执行,任何人都知道这个具体问题或者我的错误在哪里?感谢帮助!问候!

1 个答案:

答案 0 :(得分:1)

$stmt = $dbh->prepare("INSERT INTO products(product_name)
                VALUES(:productName)");
$stmt->bindParam(':productName', $productName);

$productName = $username;

在$ productName为NULL时绑定它,然后在绑定后为其赋值。在绑定之前为productName分配值:

$productName = $username;

$stmt = $dbh->prepare("INSERT INTO products(product_name)
                VALUES(:productName)");
$stmt->bindParam(':productName', $productName);

修改

//check $username
var_dump($username);
$test = array('cmd'=>'_xclick',
                 ....
                'custom'=>$crypt->encrypt(serialize(array("username" => $username))));
                 ....

...
$custom = unserialize($crypt->decrypt($_POST["custom"]));
//check $custom
var_dump($custom);