如何使用php将表单值发布到url和数据库

时间:2013-07-03 18:34:20

标签: php forms url post

我需要有关如何将表单数据发布到也发送到数据库的URL的帮助。这是我开发的代码,但它似乎没有工作,它没有将信息发送到数据库和卷曲

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.

这是我的CURL

//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 

1 个答案:

答案 0 :(得分: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个必要字段,运行测试,并检查数据库。有没有数据进入?然后,构建它并确保整个记录正确输入。

最后,一旦你确定前两个工作完美,那么请为你的祈祷处理器添加代码。