-5

请我需要有关如何将表单数据发布到也进入数据库的 url 的帮助。这是我开发的代码,但它似乎不起作用,它没有将信息发送到数据库和 curl

include_once('Libraries/libraries.php');

//send the info to this page via POST method.

$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$address = $_POST['address'];
$state = $_POST['price_2'];
$v_merchant_id = $_POST['v_merchant_id'];
$merchant_ref = $_POST['merchant_ref'];
$memo = $_POST['memo'];
$item_1 = $_POST['item_1'];
$price_1 = $_POST['price_1'];
$item_2 = $_POST['item_2'];
$price_2 = $_POST['price_2'];
$notify_url =$_POST['notify_url'];
$success_url = $_POST['success_url'];
$fail_url = $_POST['fail_url'];
$total= $_POST['price_2'] + $_POST['price_1'];

if ($price_2 == 'Lagos'){
    $item_2='COST OF DELIVERY WITHIN LAGOS';
    $price_2='';}
elseif ($price_2 !='lagos'){
    $item_2='COST OF DELIVERY OUTSIDE LAGOS';
    $price_2='5000';};
//connect to database a run sql "INSERT INTO" query.

if (isset($_POST['name'])){
$sql = "INSERT INTO customers (name,phone,email,address,price_2,v_merchant_id,merchant_ref,memo,item_1,price_1,total) 
VALUES (:name,:phone,:email,:address,:price_2,:v_merchant_id,:merchant_ref,:memo,:item_1,:price_1,:total)";
$q = $conn->prepare($sql);
$q->bindParam(':name', $name, PDO::PARAM_STR);
$q->bindParam(':phone', $phone, PDO::PARAM_STR);
$q->bindParam(':email', $email, PDO::PARAM_STR);
$q->bindParam(':address', $address, PDO::PARAM_STR);
$q->bindParam(':price_2', $price_2, PDO::PARAM_STR);
$q->bindParam(':v_merchant_id', $v_merchant_id, PDO::PARAM_STR);
$q->bindParam(':merchant_ref', $merchant_ref, PDO::PARAM_STR);
$q->bindParam(':memo', $memo, PDO::PARAM_STR);
$q->bindParam(':item_1', $item_1, PDO::PARAM_STR);
$q->bindParam(':price_1', $price_1, PDO::PARAM_STR);
$q->bindParam(':total', $total, PDO::PARAM_STR);
$q->execute();
};


$loc = 'location:https://voguepay.com/pay/?v_merchant_id='.$v_merchant_id.'&merchant_ref='.$merchant_ref.'&memo='.$memo.'&item_1='.$item_1.'&price_1='.$price_1.'&item_2='.$item_2.'&price_2='.$price_2.'&notify_url='.$notify_url.'&success_url='.$success_url.'&fail_url='.$fail_url.'';
header($loc);
//that last line redirects the user to the api site submitting the username.

这是我的卷曲

//preparing the posted value as argument and value for the sms gateway
$postValue = "v_merchant_id=".$v_merchant_id."&merchant_ref=".$merchant_ref."&memo=".$memo."&item_1=".$item_1."&price_1=".$price_1."&item_2=".$item_2."&price_2=".$price_2."&notify_url=".$notify_url."&success_url=".$success_url."&fail_url=".$fail_url.""; 
//the sms gateway address provided by your gateway

$apiUrl = "https://voguepay.com/pay/";
//next is to fake a browser form submitted 
//firefox is our choosing browser
$browserType ="Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";

//initiating the curl library
$ci = curl_init(); 

//set the url to be used  for processing the data
curl_setopt($ci, CURLOPT_URL,$apiUrl); 
curl_setopt($ci, CURLOPT_HEADER, true);
curl_setopt($ci, CURLOPT_FOLLOWLOCATION, false);

//set our browser type that has been initiad before
curl_setopt($ci, CURLOPT_USERAGENT, $browserType);

//set the maxmium time to execute the script before timing out
curl_setopt($ci,CURLOPT_CONNECTTIMEOUT,2); 

//accept the response after the execution 
curl_setopt($ci, CURLOPT_RETURNTRANSFER, true); 

// set the post method for passing variables to the server 
curl_setopt($ci, CURLOPT_POST, 1);

//assigning the values to be posted to the sms gateway
curl_setopt($ci, CURLOPT_POSTFIELDS, $postValue);

//execute the function and get result from the gateway
$a=curl_exec ($ci);

curl_close ($ci); // close the connection 
4

1 回答 1

0

首先,将您的代码分成几个部分并测试每个部分。

首先,测试您是否正确接收到数据。一个错误可能会阻止整个页面的处理,因此请确保您收到的是您认为收到的内容:

$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
...etc...
$zz = 'RECEIVED DATA:<br /><br />';
$zz .= 'Name: [' .$name. ']';
$zz .= 'Phone: [' .$phone. ']';
$zz .= 'Email: [' .$email. ']';
echo $zz;
die();

接下来,确保您的 MySQL 插入工作正常。将代码剥离到只有 3 或 4 个基本字段,运行测试并检查数据库。有数据进去吗?然后,建立它并确保正确输入整个记录。

最后,一旦您确定前两个工作正常,然后为您的祈祷处理器添加代码。

于 2013-07-03T18:50:25.097 回答