2015-09-13 117 views
0
$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']; 
     $txn_type = $_POST['txn_type']; 
     $receiver_email = $_POST['receiver_email']; 
     $payer_email = $_POST['payer_email']; 
     $pay_date = $_POST['payment_date']; 
     $payment_type = $_POST['payment_type']; 


     // Personal information 
     $firstname = $_POST['first_name']; 
     $lastname = $_POST['last_name']; 

     if (isset($_POST['payer_business_name'])) 
      $business = $_POST['payer_business_name']; 
     else 
      $business = ""; 

     $address_zip = $_POST['address_zip']; 
     $address_street = $_POST['address_street']; 
     $address_city = $_POST['address_city']; 
     $address_state = $_POST['address_state']; 
     $address_country = $_POST['address_country']; 
     $address_country_code = $_POST['address_country_code']; 

     if (isset($_POST['contact_phone'])) 
      $payer_phone = $_POST['contact_phone']; 
     else 
      $payer_phone = "";  

$array = array(
       ":txn_id" => $txn_id, 
       ":payer_firstname" => $firstname, 
       ":payer_lastname" => $lastname, 
       ":payer_business" => $business, 
       ":payer_email" => $payer_email, 
       ":payer_country_code" => $address_country_code, 
       ":payer_country" => $address_country, 
       ":payer_state" => $address_state, 
       ":payer_city" => $address_city, 
       ":payer_zip" => $address_zip, 
       ":payer_street" => $address_street, 
       ":payer_phone" => $payer_phone, 
       ":payer_type" => $payment_type, 
       ":item_name" => $item_name, 
       ":item_number" => $item_number, 
       ":payment_amount" => $payment_amount, 
       ":payment_status" => $payment_status, 
       ":payment_currency" => $payment_currency, 
       ":date" => $pay_date, 
       ":ip" => $client->IP() 
     ); 


     $prepare = $conn->prepare("INSERT INTO transactions (t_num, t_payer_firstname, t_payer_lastname, t_payer_business, t_payer_email, t_payer_country_code, t_payer_country, t_payer_state, t_payer_city, t_payer_street, t_payer_zip, t_payer_phone, t_payer_type, t_name, t_hex, t_price, t_status, t_currency, t_date, t_ip) VALUES (:txn_id, :payer_firstname, :payer_lastname, :payer_business, :payer_email, :payer_country_code, :payer_country, :payer_state, :payer_city, :payer_zip, :payer_street, :payer_phone, :payer_type, :item_name, :item_number, :payment_amount, :payment_status, :payment_currency, :date, :ip) "); 

     if ($prepare->execute($array)) { 
      $msg = "<strong>Congratulations " . $lastname . ", " . $firstname . "!</strong> 
      <br/>Your order has been placed and will be processed within 12-24 hours! 
      <br/><br/> 
      <b>Transaction #:</b> " . $txn_id . "<br/> 
      <b>Item Name:</b> " . $item_name . "<br/> 
      <b>Item Number:</b> " . $item_number . "<br/><br/><br/> 
      <b>First Name:</b> " . $first_name . "<br/> 
      <b>Last Name:</b> " . $last_name . "<br/> 

      "; 
     } 

出於某種原因將兩次,我的查詢將自身插入兩次....我不知道這是否是if語句檢查,如果rowCount時大於0,這是個問題還是我瞎了點什麼?PDO查詢連續

編輯:編輯後顯示$ _POST變量。

+1

這是一個松鼠一個......它看起來好像沒什麼問題。我可以提供的唯一建議是,您可能想使用'$ conn-> lastInsertId()'而不是'$ execute-> rowCount()'來檢查查詢是否成功?也許在你可能沒有在這裏顯示的干預代碼中有一些東西? – Ragdata

+0

問題是,這是我的代碼中唯一需要數據庫工作atm的東西......這是我第一個實際的SQL語句,所以沒有真正的干預代碼甚至可以允許這樣做。 感謝您對lastInsertId的建議,我現在已經開始使用它,但我仍然以某種方式得到相同的問題:( –

+0

如何更高一點?我假設會有某種條件表達式需要滿意爲了執行此代碼 - 問題可以在那裏嗎? – Ragdata

回答

0

不知何故你正在執行查詢兩次,因爲下面是我嘗試使用的代碼,它總是插入一行。

還檢查每個包含索引文件。或者重新啓動mysql。

<?php 
$array = array(
     ":txn_id" => "1", 
     ":payer_firstname" => "2", 
     ":payer_lastname" => "3", 
     ":payer_business" => "4", 
     ":payer_email" => "5", 
     ":payer_country_code" => "6", 
     ":payer_country" => "7", 
     ":payer_state" => "8", 
     ":payer_city" => "9", 
     ":payer_zip" => "10", 
     ":payer_street" => "11", 
     ":payer_phone" => "12", 
     ":payer_type" => "13", 
     ":item_name" => "14", 
     ":item_number" => "15", 
     ":payment_amount" => "16", 
     ":payment_status" => "17", 
     ":payment_currency" => "18", 
     ":date" => "19", 
     ":ip" => "20" 
); 

$conn = new PDO('mysql:host=localhost;dbname=test', "root", "123456"); 

$prepare = $conn->prepare("INSERT INTO transactions (t_num, 
t_payer_firstname, t_payer_lastname, t_payer_business, t_payer_email, 
t_payer_country_code, t_payer_country, t_payer_state, t_payer_city, 
t_payer_street, t_payer_zip, t_payer_phone, t_payer_type, t_name, 
t_hex, t_price, t_status, t_currency, t_date, t_ip) VALUES (:txn_id, 
:payer_firstname, 
:payer_lastname, 
:payer_business, 
:payer_email, 
:payer_country_code, 
:payer_country, 
:payer_state, 
:payer_city, 
:payer_zip, 
:payer_street, 
:payer_phone, 
:payer_type, 
:item_name, 
:item_number, 
:payment_amount, 
:payment_status, 
:payment_currency, 
:date, 
:ip) "); 

$execute = $prepare->execute($array); 

if ($execute) { 
    echo 'good'; 
} 

?>

+0

我已經完成了上面所說的所有內容我仍然遇到問題......也許這是我的$ _POST的東西? –