2014-06-08 197 views
0

我有簡單的付款方式,它包括voucher_id,customer_id,amount,payment_type,transaction_typedatephp mysql(插入查詢)

Transaction_type的取款和存款。

我的數據庫表不應該包含「金額」列。它有信用卡和借記欄。在這種情況下,如果INSERT的金額爲debit列,如果交易type ="Withdraws"INSERT金額爲信用列Transaction_type ="Deposits"

當我INSERT數據表顯示:

列數在列不匹配值計數1

因爲,表中有借方和貸方列。但是我的表單有一列,名字就是金額。如何插入此查詢來理清我的問題。

<?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(); 

    } 



?> 
+1

你能分享一些代碼嗎? – shmosel

+0

請幫我..這是我的最後一個項目。 – user3669821

回答

0

由於您沒有爲每列指定值,因此必須指定要更新的列。即使在更新所有列時,這也是一種很好的做法,因此您不必擔心未來對錶結構的更改(例如,添加或移動列)。

您還沒有列入表結構,因此我不得不去猜測你的列名:

 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.'")'; 

     } 

我想有你離開了存款transaction_typebalance理由嗎?

+0

是的..事實上,我從來沒有寫這樣的INSERT查詢..我總是使用直接格式..我得到那..shmosel,非常感謝。我糾正了我的錯誤.. – user3669821

0

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'); 

檢查您的查詢並修復它,使列數和值匹配。

+1

事實上,這就是爲什麼你應該總是指定你的列:D –

+0

好像他將一個金額字段插入到具有'credit'和'debit'列的表中。肯定會有助於看到一些代碼... – shmosel

+0

我的表單有文本框,它的名字是「金額」。該金額將一次插入信用卡或借記欄..但表中有借方和貸記欄。爲此顯示此錯誤..列計數不匹配... – user3669821