Hassan Muhammad
Hassan Muhammad

Reputation: 11

Hi what i am doing wrong with this query?

I want to select the income amount from income table, update the total income and subtract the income from the total income. Then delete the row.

public function delete($id)
{ 
    $this->db->get('income');
    $this->db->select('income_amount');
    $this->db->query('update total_income set amount=amount-'.$this->db->select('income_amount'));

    $this->db->where('income_id', $id);
    $this->db->delete('income');    
}

Upvotes: 1

Views: 47

Answers (1)

Bryan Lee
Bryan Lee

Reputation: 149

Unless I have misunderstood your question, you want to do the following :

  1. Select income_amount from the income table :

    $this->db->select('income_amount');
    $query = $this->db->get('income');
    $row = $query->row();
    
    if (isset($row)) {
        $income_amount = $row->income_amount;
    }
    
  2. Update the total_income table by substracting $income_amount from all the amount values of the table :

    $this->db->set('amount', 'amount-'.$income_amount);
    $this->db->update('total_income');
    
  3. Delete the row identified by $id from the income table :

    $this->db->delete('income', array('income_id' => $id));
    

Upvotes: 1

Related Questions