CodeIgniter Forums

Full Version: Insert ID for an AR Update?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]codemonkey[/eluser]
See title.

I don't think insert_id() works with $this->db->update()

Am I wrong? Is there another way?

Thanks

El Forum

[eluser]GrahamDj28[/eluser]
If you update a record, then the ID is already known.
How else would you update the correct record?

And as the name implies, it returns the record ID on an insert. Not on an update

El Forum

[eluser]codemonkey[/eluser]
[quote author="GrahamDj28" date="1336084875"]If you update a record, then the ID is already known.
How else would you update the correct record?

And as the name implies, it returns the record ID on an insert. Not on an update[/quote]


I see why it wouldn't work.

How can I grab the ID from a loop that checks to see if a value is already in the db?

Example:

Code:
$q = $this
        ->db
        ->where('item_num', $num_to_check)
        ->limit(1)
        ->get('items');

if ( $q->num_rows == 1) {
//then UPDATE
}
//add

El Forum

[eluser]GrahamDj28[/eluser]
Well, if you want to make sure that a value is unique you can set your DB table column to unique.
Inserting the value for a second time will then fail. So the executed query should return FALSE.

Could you maybe explain what you actually want to do?