简体   繁体   中英

Getting the rowid after adding an item with CI cart?

On the codeigniter website it says the the insert() method will return a $rowid of the latest inserted product. However How exactly do I grab it?

$data = array();

$insert = $this->cart->insert($data);

I tried $insert['rowid'] and $insert->rowid but neither seem to work.

Thank you!

introducing: insert_id()

like this

$id = $this->db->insert_id();
return $id;

in your model, lets call it friend_model

function insertRow()
{
// Prepare data, normally you would pass this in
$data = array(
'first' => 'john',
'last' => 'smith'
);
// insert data
$this->db->insert( 'friends', $data );
// confirm insert       
if ( $this->db->affected_rows() == '1' )
// return new ID
{ $id = $this->db->insert_id();
return $id; }
// else did not insert, return false 
else {return FALSE;}
}

in your controller, check if you got an id back from model

if(! $id = $this->friend_model->insertRow() )
// it no work
{ // some error method 
}
else
{ // success ! 
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM