CodeIgniter Forums
REACTOR $this->db->set() not working in update - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: REACTOR $this->db->set() not working in update (/showthread.php?tid=38512)



REACTOR $this->db->set() not working in update - El Forum - 02-10-2011

[eluser]Pedro Luz[/eluser]
Hi,

Im having problems with the when doing updating. (REACTOR)

Code:
$this->db->where('id', $id);
$this->db->set('field', $data);
$this->db->update('table');

Even if i do

Code:
$this->db->limit(1);

It updates all the records in the table. Sad

any ideas
thanks.


REACTOR $this->db->set() not working in update - El Forum - 02-10-2011

[eluser]LuckyFella73[/eluser]
The user guide says nothing about using "SET" when updating.
Don't know if it should work like that.

Just try this:
Code:
$data = array(
    'field' => $your_data
);

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

assuming $your_data (in your example '$data') is a variable and no array


EDIT: re reading the User Guide I see that you can use "SET" as well ..

If that code doesn't work please provide more of your code


REACTOR $this->db->set() not working in update - El Forum - 02-10-2011

[eluser]Pedro Luz[/eluser]
Quote:The user guide says nothing about using “SET” when updating.

It does. In the $this->db->update() documentation, the last line says...

Quote:You may also use the $this->db->set() function described above when performing updates.

I know it works by passing and array like u explained.

But it used to work with $this->db->set(), because i used to do that way. In 1.7.2

My code is just:

Code:
$id = 5;
$data = 'pimpampum';
$this->db->where('id', $id);
$this->db->set('field', $data);
$this->db->update('table');



REACTOR $this->db->set() not working in update - El Forum - 02-10-2011

[eluser]LuckyFella73[/eluser]
I did set up a test db table and fired your code.
Only the row with the specified id was updated.

Are you sure your rows don't have the same id?
Otherwise I don't know what could be wrong.


REACTOR $this->db->set() not working in update - El Forum - 02-10-2011

[eluser]Jaketoolson[/eluser]
Wouldn't it be beneficial if you were to first check the query to make sure its valid, without using CI's DB? Doesn't anyone use MYSQL Query Browsers, phpMyAdmin, or other interfaces to interact with their databases? If this were me, and I had this problem, I'd first try writing the query out to see if it works.

Code:
"UPDATE table SET field=$data WHERE id=$id";

If this still updates everything then it's your DB structure.