Welcome Guest, Not a member yet? Register   Sign In
help, updating excisting row in database[SOLVED]
#1

[eluser]Ivar89[/eluser]
hi,

I recently started using codeIgniter and I am lost atm.
I need to update the last row in my database, this is my code so far in model:
Code:
$sql = $this->db->query ('Select * From tblpage Where ID_Page = (Select Max(ID_Page) From tblpage)');
        $id['ID_Page'] = $sql;
        
        if(isset($data['strPageUrl']))
            $this->db->set('strPageUrl',$data['strPageUrl']);

        if(isset($data['strTitle']))
            $this->db->set('strTitle',$data['strTitle']);

        if(isset($data['strPageName']))
            $this->db->set('strPageName',($data['strPageName']));
            
        $this->db->where('ID_Page',$id);
        $query = $this->db->update('tblpage');
        
        return $this->db->affected_rows();
this is my controler:
Code:
{
        $this->insert_model->Insert_Description($_POST);
        $this->load->view('page/step3');
    }
Now if I delete; $this->db->where('ID_Page',$id); it updates all the table.
If it stays there I get a database error:

how can I tell him to just update the LAST row?

thanks,
#2

[eluser]danmontgomery[/eluser]
Code:
$id['ID_Page'] = $sql;

This shouldn't be an array... Furthermore, you can't pass a database result to where() (or any of the active record functions, for that matter). It would just be easier to limit and order your update query:

Code:
$this->db->order_by('ID_Page', 'DESC')->limit(1)->update($table_name, array('field_to_update' => 'new_value'));
#3

[eluser]Ivar89[/eluser]
Code:
$id['ID_Page'] = $sql;

Yhea I notced right after I posted that it wouldn't work...
I fixed it differently because if multiple people were editing the databes they would both be editing the last row so I fixed it using a hidden field which saves the insert id and I just post it with every update.

Code:
Return $this->db->insert_id();




Theme © iAndrew 2016 - Forum software by © MyBB