2

I am able to get the last inserted id using $this->db->insert_id(); in codeigniter, is there any way that I can get the id of the last updated record? I tried it with the same i.e. $this->db->insert_id(); but it doesn't work (returns 0 instead).

Kamran Ahmed
  • 11,809
  • 23
  • 69
  • 101

6 Answers6

7

Codeigniter doesn't support that. I had to do this:

$updated_id = 0;

// get the record that you want to update
$this->db->where(array('vrnoa'=>$data['vrnoa'], 'etype' => 'sale'));
$query = $this->db->get('StockMain');

// getting the Id
$result = $query->result_array();
$updated_id = $result[0]['stid'];

// updating the record
$this->db->where(array('vrnoa'=>$data['vrnoa'], 'etype' => 'sale'));
$this->db->update('StockMain',$data);
Kamran Ahmed
  • 11,809
  • 23
  • 69
  • 101
4
$this->db->insert_id();  

this will give only inserted id. For getting the updated row id you can add a column as lastmodified (timestamp) and update this column with current timestamp everytime you run the update query. After your update query just run this:

$query = $this->db->query('SELECT id FROM StockMain ORDER BY lastmodified DESC LIMIT 1');  
$result = $query->result_array();  

You will get the id in the result set.

user2936213
  • 1,021
  • 1
  • 8
  • 19
2

Here is how you can do it shortest

$where  =   array('vrnoa'=>$data['vrnoa'], 'etype' => 'sale');

//Update the record

$this->db->where($where);
$this->db->update('StockMain',$data);

//Get the record

$this->db->where($where);
return $this->db->get('StockMain')->row()->stid;
Muhammad Raheel
  • 19,823
  • 7
  • 67
  • 103
1

Return the id which your using in where clause for update

function Update($data,$id){
        $this->db->where('id', $id);
        $this->db->update('update_tbl',$data);
        return $id; 
    }
Mithun Billara
  • 140
  • 1
  • 6
0

Using a codeigniter , and MY_MODEL an extend version.This was one of the bottlneck this how i got a relfe.

  function update_by($where = array(),$data=array())
  {
        $this->db->where($where);
        $query = $this->db->update($this->_table,$data);
        return $this->db->get($this->_table)->row()->id; //id must be exactly the name of your table primary key
  }

call this Updates plus get the updated id. kinda overkill to run a query twice i guess but so do all the aboves.

how you call ?

 $where = array('ABC_id'=>5,'DEF_ID'=>6);
 $data =  array('status'=>'ACCEPT','seen_status' =>'SEEN');
 $updated_id= $this->friends->update_by($where,$data);
Develop4Life
  • 7,581
  • 8
  • 58
  • 76
0

Try like this:

  //update
    public function update($table, $where, $data)
    {
        // get the record that you want to update
        $this->db->where($where);
        $query = $this->db->get($table);

        // getting the Id
        $row = array_values($query->row_array());
        $updated_id = $row[0];

        // updating the record
        $updated_status = $this->db->update($table, $data, $where);

        if($updated_status):
            return $updated_id;
        else:
            return false;
        endif;
    }
Bablu Ahmed
  • 4,412
  • 5
  • 49
  • 64