dtrn74832 2014-11-27 07:54
浏览 45

我似乎无法找到为什么这个IPN代码没有更新数据库

I ran this code without any errors but the database is not updating. I am receiving the variable data from Paypal correctly. To verify the Post data I added two lines at the beginning of the script to write the variables to a text file. The server uses php 5.4. I hope you guys can find the problem.

<?php  

/* checking for POST variables & writing to text file */
$posted_data = print_r($_POST,true);
file_put_contents('IPN_data.txt',$posted_data);

/* 
Database config here

*/

/* Connect to database */

$conn = new mysqli($db_host, $db_user, $db_pass, $db_database);
if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        } 
//read the post from PayPal system and add 'cmd' 
$req = 'cmd=_notify-validate'; 

foreach ($_POST as $key => $value) { 

$value = urlencode(stripslashes($value)); 
$req .= "&$key=$value"; 
} 

//post back to PayPal system to validate 
$header = "POST /cgi-bin/webscr HTTP/1.1
"; 
$header .= "Content-Type: application/x-www-form-urlencoded
"; 
$header .= "Host: www.sandbox.paypal.com
"; 
$header .= "Connection: close
"; 
$header .= "Content-Length: " . strlen($req) . "

"; 
$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30); 
// 

$item = $_POST['item_name'];
$transaction_id = $_POST['txn_id'];
$payeremail = $_POST['payer_email'];

//error connecting to paypal 
if (!$fp) { 
// 
} 

//successful connection     
if ($fp) { 
fputs ($fp, $header . $req); 

while (!feof($fp)) { 
    $res = fgets ($fp, 1024); 
    $res = trim($res); //NEW & IMPORTANT 

    if (strcmp($res, "VERIFIED") == 0) { 
        //insert order into database     
     if (strcmp ($payment_status, "Completed") == 0) {

           /* update database */
           if($item == 'Learn HTML'){
                    $sql = "INSERT INTO dc_html (transaction_id,email_id)
                    VALUES ( '$transaction_id','$payeremail')";
                    $conn->close();
                    break;

           }

           if($item == 'Learn Css') {
                    $sql = "INSERT INTO dc_css (transaction_id,email_id)
                    VALUES ( '$transaction_id','$payeremail')";
                    $conn->close();
                    break;
           }



      }





    } 

    if (strcmp ($res, "INVALID") == 0) { 
        //insert into DB in a table for bad payments for you to process later 
    } 
} 

fclose($fp); 
} 

?> 
  • 写回答

1条回答 默认 最新

  • dongtang4019 2014-11-27 08:04
    关注

    You seem to assign query to $sql variable but it does not appear in the code where you actually call a $conn->query($sql).

    If you do not execute the query how could it get inserted into database?

    Also adding some logging might help you debug more easily.

    Like send an email or write an error to a file if

    • postback fails
    • db connection fails
    • db query execution fails

    Hope this helps.

    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据