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 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');
    
    点赞 评论 复制链接分享
  • dongzouqie4220 dongzouqie4220 2016-06-08 09:30

    try this

    public function delete($id){
    
    $data = array(
        'a.modified_by' => $this->ion_auth->user()->row()->id,
        'a.date_modified' => date("Y-m-d"),
        'b.modified_by' => $this->ion_auth->user()->row()->id,
        'b.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');
    
    }
    
    点赞 评论 复制链接分享
  • duangang2825 duangang2825 2016-06-08 09:36
    <?php
    $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->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('a.id',  $id);
    $this->db->where('a.id = b.id');
    $this->db->update('loan_ledger as a, journal as b');
    ?>
    
    点赞 评论 复制链接分享

相关推荐