PDO查询连续两次插入

时间:2015-09-13 07:42:04

标签: php mysql

$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这是问题还是我对某些事情视而不见?

编辑:编辑帖子以显示$ _POST变量。

1 个答案:

答案 0 :(得分:0)

不知怎的,你正在执行两次查询,因为下面是我正在尝试的代码,它总是插入一行。

并检查每个include,index文件。或者重新启动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';
}

&GT;