2013-02-17 76 views
0

我想問一下,如果有可能把UPDATE的條件。首先我有兩張桌子。有條件更新記錄Codeigniter

billing_records

brecord_id (PK) 
date_billed 
monthly_rent 
water 
electricity 
total_payable 
status (paid/unpaid) 

payment_records

payment_id (PK) 
date 
type_payment (cash/cheque/on_bank) 
amount_payable 
change 
balance 
cheque_number 
bank_number 
brecord_id (FK) 

我想在代碼中插入記錄是這樣的數據庫:

IF balance==0 
    UPDATE status to 'paid' from billing_records TABLE 
else 
    UPDATE total_payable(of billing_records) = balance(of payment_records) 

在提交按鈕期間,我有兩個操作要在payment_recordsUPDATE billing_records之間插入數據。我在支付記錄中插入數據時沒有問題,只有更新了billing_records。

我有我的控制器

// 4. call model to save input to database table 

$this->m_account_statement->insertPayableRecords($data); 

$this->m_account_statement->changeStatusToPaid($brecord_id); 

在這裏,在這條線的麻煩是我的代碼:

控制器:

public function managePayment($brecord_id=0){ 

    if($this->_submit_validate_payment($this->input->post('type_payment'))===FALSE){ 

     $row = $this->m_account_statement->payableRecord($brecord_id); 

     $data['amountPayable'] = $row->result(); 
     $data['brecord_id'] = $brecord_id; 

     return $this->load->view('admin/vrecord_payment',$data); 

    } else { 

     // 2. get the inputs  

     $data['payment_id'] = $this->input->post('payment_id'); 
     $data['amount_payable'] = $this->input->post('amount_payable'); 
     $data['amount_received'] = $this->input->post('amount_received'); 
     $data['type_payment'] = $this->input->post('type_payment'); 
     $data['cheque_number'] = $this->input->post('cheque_number'); 
     $data['bank_number'] = $this->input->post('bank_number');   
     $data['balance'] = $this->input->post('balance');  
     $data['change'] = $this->input->post('change'); 
     $data['brecord_id'] = $this->input->post('brecord_id'); 
     $data['date'] = $this->input->post('date_transaction');  

     // 4. call model to save input to database table 

     $this->m_account_statement->insertPayableRecords($data); 

     $this->m_account_statement->changeStatusToPaid($brecord_id); 

     // 5. confirmation of registration 

     $this->session->set_flashdata('message',' Successfully Record Payment'); 

     redirect('caccount_statement/displayTenants'); 

    } 
} 

型號:

public function changeStatusToPaid($billing_id) 
{ 
    $this->db->query("UPDATE billing_record SET status = 'paid' WHERE brecord_id = $billing_id"); 
} 

public function insertPayableRecords ($data){ 

    if($this->db->insert('payment_record', $data)){ 
     return TRUE; 
    } 

    return FALSE; 

} 

回答

0

它是可以把一個條件你可以使用這個流程。

if balance==0 

//then fetch a record for status 
//means run a query to get status 

//now update 

else 
// select some different values for update 
// now update query with fetched values 
0
public function managePayment($brecord_id=0){ 
    //check if id is present or not like this: 
    if ($brecord_id > 0) {//Update 
     // ... put your update code ... 
    } else { //Add 
     // ... put your add code ... 
    } 
}