如果您查看下面标记为/*********
的代码,您会在下面的代码中看到完全相同的插入查询两次。第一个代码块(朝上)按预期插入行,但是,它不是我需要它的地方,我只是把它放在那里用于调试目的。第二个代码块是我需要运行插入查询的地方。当我尝试从那里运行插入查询时,它会插入一个空行。
似乎变量一旦到达最内层的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);
}
?>