0

我有这个脚本应该将信息插入到 MySQL 表中,但由于某种原因,它只会将其保存在 ipn.log 文件中,但不会运行插入查询。这是我拥有的代码。

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

?>
4

2 回答 2

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

这将在屏幕上显示错误并报告所有类型的错误,这将简化您的编码。

我希望这是解决您的问题的良好开端。

于 2013-01-06T04:14:53.420 回答
0

我不确定我做了什么不同,但删除了表格并重新创建了它。现在它起作用了。

于 2013-01-06T04:24:18.283 回答