dougu5950 2016-06-08 09:21
浏览 96
已采纳

更新两个表Codeigniter

I'm trying to update 2 tables tbl_loanledger and tbl_journal at the same time. Both tables have the same columns such as modified_by, date_modified and deleted.

When I try to run this I enter code here I'm getting an error :

Error Number: 1052

Column 'modified_by' in field list is ambiguous

UPDATE `loan_ledger` as a, journal as b SET `modified_by` = '1', `date_modified` = '2016-06-08', `deleted` = 1 WHERE `a`.`id` = '823' AND `b`.`id` = '823'

Filename: C:\xampp\htdocs\system\system\database\DB_driver.php

Line Number: 331

Is this the right way on how to update 2 tables in codeigniter?

Model

public function delete($id){

  $data = array(
        'modified_by' => $this->ion_auth->user()->row()->id,
        'date_modified' => date("Y-m-d"),
        'deleted' => 1
      );

    $this->db->set($data);
    $this->db->where('a.id', $id);
    $this->db->where('b.id', $id);
    $this->db->update('loan_ledger as a, journal as b');

}

But if I am just updating single table, it is working, here is a the code:

public function delete($id){

  $data = array(
        'modified_by' => $this->ion_auth->user()->row()->id,
        'date_modified' => date("Y-m-d"),
        'deleted' => 1
      );
    $this->db->where('id', $id);
    $this->db->update(tbl_loanledger, $data);

}
  • 写回答

3条回答 默认 最新

  • dongshuo2752 2016-06-08 10:04
    关注

    No Use of join just write two seperate update query as

     // first 
        $this->db->set('a.modified_by', $this->ion_auth->user()->row()->id);
        $this->db->set('a.date_modified', date("Y-m-d"));
        $this->db->set('a.deleted', 1);
        $this->db->where('a.id',  $id);
        $this->db->update('loan_ledger as a');
        // second
        $this->db->set('b.modified_by', $this->ion_auth->user()->row()->id);
        $this->db->set('b.date_modified', date("Y-m-d"));
        $this->db->set('b.deleted', 1);
        $this->db->where('b.id',  $id);
        $this->db->update('journal as b');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?