Php / Pdo语句给出“0”

时间:2014-01-26 19:09:16

标签: php pdo crypt

我正在使用paypal的ipn,这就是我向paypal发送数据的方式:

require_once 'classes/Crypt.php';

$crypt = new Crypt();
$crypt->Mode = Crypt::MODE_HEX;
$crypt->Key  = '§$TERGERG§$T§34t';

$test = array('cmd'=>'_xclick',
                'business'=>'meine_email',
                'notify_url'=> 'url_zum_ipn',
                '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脚本中处理数据的方式:

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

$crypt = new Crypt();
$crypt->Mode = Crypt::MODE_HEX;
$crypt->Key  = '§$TERGERG§$T§34t';

$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 (?)");
    $stmt->bindParam(1, $value1);

    $value1 = $username;

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

我的表格中没有插入内容,但是此消息:

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

我检查变量$ username是否在开头发送,它是! 但是当我用ipn-script检查变量$ username时:

if($username == "") { // gives true

所以有人知道我的错误代码在哪里?问候!

1 个答案:

答案 0 :(得分:0)

替换

$stmt = $dbh->prepare("INSERT INTO products (product_name)
                                VALUES (?)");
$stmt->bindParam(1, $value1);

$value1 = $username;

$stmt->execute();

使用:

$stmt = $dbh->prepare("INSERT INTO products(product_name) VALUES(:productName)");
$stmt->execute([':productName' => $username]);