简体   繁体   English

PHP MySQL(插入查询)

[英]php mysql (INSERT QUERY)

I have simple payment form and it is include voucher_id , customer_id , amount , payment_type , transaction_type and date . 我有一个简单的付款表格,其中包括voucher_idcustomer_idamountpayment_typetransaction_typedate

Transaction_type's are withdrawls and deposits. Transaction_type是提款和存款。

My data base table should not contain "amount" column. 我的数据库表不应包含“金额”列。 it has credit and debit column. 它具有贷方和借方列。 In this case, I want to INSERT amount to debit column if transaction type ="Withdraws" of INSERT amount to credit column if Transaction_type ="Deposits" . 在这种情况下,如果Transaction_type ="Deposits"INSERT交易type ="Withdraws"计入贷方列,我想将金额INSERT debit列。

When I INSERT data to table show: 当我在表中INSERT数据时显示:

Column count doesn't match value count at row 1 列数与第1行的值数不匹配

Because, table has debit and credit column. 因为,表有借方和贷方列。 But my form has one column and it is name is amount. 但是我的表格只有一栏,它的名称就是金额。 How can INSERT this query to sort out my question. 如何插入此查询以解决我的问题。

<?php

// save data if form is submitted
    if(isset($_POST['submitted'])){

        // catch data
        $voucher_numebr = $_POST['voucher_numebr'];
        $member_id = $_POST['member_id'];
        $amount = $_POST['amount'];
        $acc_type = $_POST['acc_type'];
        $payment_type = $_POST['payment_type'];
        $discription = $_POST['discription'];
        $created = date("Y-m-d H:i:s");

        // $balance = $_POST['balance'];

        $tranaction_type = $_POST['tranaction_type'];
        $balance="NOT DEFINE";

            //insert  to the tbl_customer_account


            if($tranaction_type == "Withdrawls."){
                //echo "1";
                $sql ='INSERT INTO tbl_customer_account VALUES(NULL,"'.$voucher_numebr.'","'.$member_id.'","'.$amount.'","'.$acc_type.'","'.$payment_type.'","'.$discription.'","'.$created.'","'.$tranaction_type.'","'.$balance.'")';

            }
            else{
                $sql ='INSERT INTO tbl_customer_account VALUES(NULL,"'.$voucher_numebr.'","'.$member_id.'","'.$amount.'","'.$acc_type.'","'.$payment_type.'","'.$discription.'","'.$created.'")';

            }

        //else{




        //}


        mysql_query($sql, $conn) or die(mysql_error());
        echo 'form submitted'; 
        echo '<br />';
        echo mysql_affected_rows(); die();

    }



?>

Column count doesn't match value count at row ... means you have specified a different amount of columns than you have for values. Column count doesn't match value count at row ...表示您指定的Column count doesn't match value count at row ...不同。

-- correct
INSERT INTO table (col1, col2) VALUES ('val1', 'val2');

-- error: 1 column, 2 values
INSERT INTO table (col1) VALUES ('val1', 'val2'); 

-- error: 2 columns, 1 value
INSERT INTO table (col1, col2) VALUES ('val1'); 

Check your query and fix it so the number of columns and values match. 检查并修复查询,以使列数和值匹配。

Since you're not specifying a value for each column, you have to specify which columns you're updating. 由于您没有为每个列指定值,因此必须指定要更新的列。 This is a good practice even when you are updating all columns, so you don't have to worry about future changes to the table's structure (eg, adding or moving columns). 即使更新所有列,这也是一个好习惯,因此您不必担心表结构的将来更改(例如,添加或移动列)。

You haven't included the table structure, so I'll have to guess your column names: 您尚未包括表结构,因此我不得不猜测您的列名:

        if($tranaction_type == "Withdrawls."){
            //echo "1";
            $sql ='INSERT INTO tbl_customer_account (some_field, voucher_number, member_id, debits, account_type, payment_type, description, created, transaction_type, balance) VALUES(NULL,"'.$voucher_numebr.'","'.$member_id.'","'.$amount.'","'.$acc_type.'","'.$payment_type.'","'.$discription.'","'.$created.'","'.$tranaction_type.'","'.$balance.'")';

        }
        else{
            $sql ='INSERT INTO tbl_customer_account (some_field, voucher_number, member_id, credits, account_type, payment_type, description, created) VALUES(NULL,"'.$voucher_numebr.'","'.$member_id.'","'.$amount.'","'.$acc_type.'","'.$payment_type.'","'.$discription.'","'.$created.'")';

        }

I suppose there's a reason you're leaving out transaction_type and balance for deposits? 我想有一个原因您遗漏了transaction_typebalance的存款?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM