我运行此代码没有任何错误,但数据库没有更新。我正在从Paypal接收可变数据。为了验证Post数据,我在脚本的开头添加了两行来将变量写入文本文件。服务器使用PHP 5.4。我希望你们能找到问题。
<?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\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Host: www.sandbox.paypal.com\r\n";
$header .= "Connection: close\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$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);
}
?>
答案 0 :(得分:0)
您似乎将查询分配给$ sql变量,但它不会出现在您实际调用$ conn-&gt;查询($ sql)的代码中。
如果你不执行查询,它怎么能插入数据库?
同时添加一些日志记录可能会帮助您更轻松地进行调试。
如果发送电子邮件或将错误写入文件
希望这有帮助。