2013-01-06 51 views
0

我有這個腳本應該將信息插入到MySQL表中,但由於某種原因,它只會將其保存在ipn.log文件中,但不會運行插入查詢。這是我的代碼。PayPal IPN日誌但不插入MySQL

<?php 

//Lets do MySql connection stuff 
$mysql_host = 'SERVER'; //Leave at localhost 
$mysql_user = 'USER'; //DB User 
$mysql_pass = 'PASSWORD'; //DB Pass 
$mysql_db = 'DATABASE'; //DB Name 

//------------------------------------------------------------------ 
// Open log file (in append mode) and write the current time into it. 
// Open the DB Connection. Open the actual database. 
//------------------------------------------------------------------- 
$log = fopen("ipn.log", "a"); 
fwrite($log, "\n\nipn - ".gmstrftime("%b %d %Y %H:%M:%S", time())."\n"); 
$db = mysql_connect($mysql_host, $mysql_user, $mysql_pass); 
mysql_select_db($mysql_db, $db); 

//------------------------------------------------ 
// Read post from PayPal system and create reply 
// starting with: 'cmd=_notify-validate'... 
// then repeating all values sent - VALIDATION. 
//------------------------------------------------ 
$postvars = array(); 

while(list($key, $value) = each($_POST)) 
{ 
    $postvars[] = $key; 
} 
$req = 'cmd=_notify-validate'; 

for($var = 0; $var < count($postvars); $var++) 
{ 
    $postvar_key = $postvars[$var]; 
    $postvar_value = $$postvars[$var]; 
    $req .= "&".$postvar_key."=".urlencode($postvar_value); 
} 

//-------------------------------------------- 
// Create message to post back to PayPal... 
// Open a socket to the PayPal server... 
//-------------------------------------------- 
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n"; 
$header .= "Content-Type: application/x-www-form-urlencoded\r\n"; 
$header .= "Content-Length: ".strlen($req)."\r\n\r\n"; 
$fp = fsockopen("www.paypal.com", 80, $errno, $errstr, 30); 

//--------------------------------------------- 
fwrite($log, "Vals: ". $invoice." ". $receiver_email." ". $item_name." ". $item_number." ". $quantity." ". $payment_status." ". $pending_reason." ".$payment_date." ". $payment_gross." ". $payment_fee." ". $txn_id." ". $txn_type." ". $first_name." ". $last_name." ". $address_street." ". $address_city." ". $address_state . " ".$address_zip." ". $address_country." ". $address_status." ". $payer_email. " ". $payer_status." ". $payment_type." ". $notify_version." ". $verify_sign. "\ n"); 

// assign posted variables to local variables 
$item_name = $_POST['item_name']; 
$item_number = $_POST['item_number']; 
$payment_status = $_POST['payment_status']; 
$payment_amount = $_POST['mc_gross']; 
$payment_currency = $_POST['mc_currency']; 
$txn_id = $_POST['txn_id']; 
$receiver_email = $_POST['receiver_email']; 
$payer_email = $_POST['payer_email']; 

//---------------------------------------------------------------------- 
// Check HTTP connection made to PayPal OK, If not, print an error msg 
//---------------------------------------------------------------------- 
if(!$fp) 
{ 
    echo $errstr."(".$errno.")"; 
    fwrite($log, "Failed to open HTTP connection!"); 
    $res = "FAILED"; 
} 
//-------------------------------------------------------- 
// If connected OK, write the posted values back, then... 
//-------------------------------------------------------- 
else 
{ 
    fputs($fp, $header . $req); 
    //------------------------------------------- 
    // ...read the results of the verification... 
    // If VERIFIED = continue to process the TX... 
    //------------------------------------------- 
    while(!feof($fp)) 
    { 
    $res = fgets($fp, 1024); 
    if(strcmp($res, "VERIFIED") == 0) 
    { 
     //-------------------------------------- 
     // Insert Transaction details into DB. 
     //-------------------------------------- 
     $qry = "INSERT INTO DONATIONS (invoice, receiver_email, item_name, item_number, quantity, payment_status, pending_reason, payment_date, payment_gross, payment_fee, txn_id, txn_type, first_name, last_name, address_street, address_city, address_state, address_zip, address_country, address_status, payer_email, payer_status, payment_type, notify_version, verify_sign) VALUES('$invoice', '$receiver_email', '$item_name', '$item_number', '$quantity', '$payment_status', '$pending_reason', '$payment_date', '$payment_gross', '$payment_fee', '$txn_id', '$txn_type', '$first_name', '$last_name', '$address_street', '$address_city', '$address_state', '$address_zip', '$address_country', '$address_status', '$payer_email', '$payer_status ', '$payment_type', '$notify_version', '$verify_sign')"; 
     $result = mysql_query($qry); 
    } 
    } 
} 
//------------------------------------------- 
// Close PayPal Connection, Log File and DB. 
//------------------------------------------- 
fclose($fp); 
fclose($log); 
mysql_close($db); 

?> 
+0

可能是sql中的錯誤 - 嘗試添加'echo mysql_error();'在'$ result ='行後 – Sp4cecat

+0

當我這樣做時沒有錯誤返回。 – austinhollis

+0

這是一些*糟糕的* PHP代碼。你在哪裏挖掘它? – Charles

回答

1

,最好的辦法是把一些測試代碼過於找出「某種原因」,看看那裏的錯誤是:

如果您連接到MySQL數據庫:

$db = mysql_connect($mysql_host, $mysql_user, $mysql_pass); 
if (!$db) { 
    die('Could not connect: ' . mysql_error()); 
} else { 
$db_selected = mysql_select_db($mysql_db, $db); 
if (!$db_selected) { 
    die ('Can\'t use selected db : ' . mysql_error()); 
    } 
} 

而且,你在數據庫中插入數據:

$result = mysql_query($qry); 
    if (!$result) { 
    die('Could not insert in db: ' . mysql_error()); 
    } 

現在你可以找出原因的信息中沒有輸入通過查看錯誤的數據庫。

此外,一定要添加到您的PHP文件的開頭而發展中國家在屏幕上顯示的錯誤:

<?php 
ini_set('display_errors', 1); error_reporting(E_ALL); 

這將顯示在屏幕上的錯誤,並報告所有類型的錯誤,它會減輕你的編碼。

我希望這是解決您的問題的良好開端。

0

我不完全確定我做了什麼不同,但放棄了桌子並重新創建了它。現在它可以工作。