zen
zen

Reputation: 393

Update two tables 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);

}

Upvotes: 1

Views: 10972

Answers (3)

Saty
Saty

Reputation: 22532

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

Upvotes: 4

Pervez
Pervez

Reputation: 576

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

Upvotes: 0

Keyur Chavda-kc1994
Keyur Chavda-kc1994

Reputation: 1045

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

}

Upvotes: 1

Related Questions