2014-01-31 99 views
0

嘿,我有在IPN過程中我DATABSE插入定製數據的問題,這是該方案,我使用http_build_query發送數據:PDO在IPN犯規插入

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

$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-腳本,我使用PDO bindParam插入數據:

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

$crypt = new Crypt(); 
$crypt->Mode = Crypt::MODE_HEX; 
$crypt->Key = '[email protected]#$%&*()_+?:'; 

$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 

數據發送肯定,我指向貝寶之前使用回聲檢查它。 我的表產品:

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

此刻我真的不知道爲什麼執行的查詢心不是,任何人know's這一特定問題或在我的錯誤?感謝幫助!問候!

回答

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

$productName = $username; 

您綁定$ productName而它有NULL,然後綁定後,您爲其分配值。結合之前將值分配給產品名稱:

$productName = $username; 

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

編輯:

​​
+0

感謝答案,我改變了代碼,但我仍然得到了同樣的錯誤,這似乎是「慣例」不發, 有任何想法嗎 ? – user3235284

+0

你確定你的productName沒有設置爲null嗎?嘗試var_dump($ productName)。 ''custom'=> $ crypt-> encrypt(serialize(array(「username」=> $ username)));'什麼是$ username? – CrazySabbath

+0

var_dump返回0:/ – user3235284