Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php PayPal IPN记录但不';t在MySQL中插入_Php_Mysql_Paypal Ipn - Fatal编程技术网

Php PayPal IPN记录但不';t在MySQL中插入

Php PayPal IPN记录但不';t在MySQL中插入,php,mysql,paypal-ipn,Php,Mysql,Paypal Ipn,我有一个脚本,它应该将信息插入MySQL表中,但由于某种原因,它只会将信息保存在ipn.log文件中,而不会运行insert查询。这是我的代码 <?php //Lets do MySql connection stuff $mysql_host = 'SERVER'; //Leave at localhost $mysql_user = 'USER'; //DB User $mysql_pass = 'PASSWORD'; //DB Pass $mysql_db = 'DATA

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

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

?>

最好的方法是通过在代码中进行一些测试来找出“某些原因”,看看错误在哪里:

连接到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);  

我不太清楚我做了什么不同的事情,但删除了表并重新创建了它。现在它可以工作了。

可能是sql中的错误-请尝试添加“echo mysql_error();”在“$result=”行之后,我这样做时没有返回任何错误。这是一些糟糕的PHP代码。你在哪里找到它的?我不记得网站了…好吧,在$result之后,我们会回显实际的$qry变量-这将a)让你知道它实际上正在运行,b)然后你可以将该查询粘贴到PHP myadmin或其他东西中,看看它是否在那里工作。此外,确保顶部的四个变量设置正确。