如果您查看标记为:的代码/*********
,您将看到相同的插入查询在下面的代码中出现了两次。第一个代码块(朝向顶部)按预期插入行,但是,它不是我需要的位置,我只是将它放在那里用于调试目的。第二个代码块是我需要从中运行插入查询的地方。当我尝试从那里运行插入查询时,它会插入一个空白行。
似乎变量一旦到达最里面的 if 语句就会失去它们的值,我无法给出一个合乎逻辑的解释来解释它为什么这样做。
我试图解决这个问题:
***添加mysql_real_escape_string
到我存储的变量中
***从我的插入查询中删除了 AUTO_INCREMENT id 字段
***尝试从所有嵌套的 if 语句中插入字符串文字 - 效果很好,但是一旦我将这些字符串文字更改为我需要的变量,它就会插入一个空白行。
***在查询中添加或死亡(mysql_error()),没有产生错误。
请提供建议,因为让我正常工作至关重要......
注意:我从来没有在同一个脚本中运行两个插入查询,我只是在下面的脚本中添加了相同的“插入”代码块两次,以使读者清楚我试图让它工作,以及脚本如何响应因此。
<?php
session_start();
//Create mysql connect variable
$conn = mysql_connect('myhostedresource.com', 'user', 'password');
//kill connection if error occurs
if(!$conn){
die('Error: Unable to connect.' . '<br>' . mysql_error());
}
//connect to mysql database
mysql_select_db("stevenash", $conn);
// PHP 4.1
// 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.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);
// 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'];
$usr_email = mysql_real_escape_string($_SESSION['email']);
$usr_compname = mysql_real_escape_string($_SESSION['compname']);
$usr_city = mysql_real_escape_string($_SESSION['city']);
$usr_state = mysql_real_escape_string($_SESSION['state']);
$usr_phone = mysql_real_escape_string($_SESSION['phone']);
$usr_password = mysql_real_escape_string($_SESSION['password']);
$usr_first = mysql_real_escape_string($_SESSION['first']);
$usr_last = mysql_real_escape_string($_SESSION['last']);
//********************************************************
$myString = "INSERT users (`email`, `compname`, `city`, `state`, `phone`, `password`, `first`, `last`)
VALUES('".$usr_email."','".$usr_compname."','".$usr_city."','".$usr_state."','".$usr_phone."','".$usr_password."','".$usr_first."','".$usr_last."')";
echo $myString;
mysql_query($myString,$conn);
//********************************************************
if (!$fp) {
// HTTP ERROR
}else{
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
// check the payment_status is Completed
if($payment_status == 'Completed'){
$txn_id_check = mysql_query("SELECT txn_id FROM log WHERE txn_id='$txn_id'");
//add user to database
if(mysql_num_rows($txn_id_check) != 1){ //payment has not been processed yet
//continue processing
if($receiver_email=='mat9@gmail.com'){
if($payment_amount=='1.00' && $payment_currency=='USD'){
//add transaction id to db
mysql_query("INSERT INTO log (`log_id`, `txn_id`, `email`) VALUES('','$txn_id','$payer_email')", $conn);
//********************************************************
$myString = "INSERT users (`email`, `compname`, `city`, `state`, `phone`, `password`, `first`, `last`)
VALUES('".$usr_email."','".$usr_compname."','".$usr_city."','".$usr_state."','".$usr_phone."','".$usr_password."','".$usr_first."','".$usr_last."')";
echo $myString;
mysql_query($myString,$conn);
//********************************************************
}
}
}
}
// check that txn_id has not been previously processed
// check that receiver_email is your Primary PayPal email
// check that payment_amount/payment_currency are correct
// process payment
}
else if (strcmp ($res, "INVALID") == 0) {
// log for manual investigation
}
}
fclose ($fp);
}
?>