CodeIgniter Forums
$id & database issue - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: $id & database issue (/thread-1864.html)



$id & database issue - El Forum - 07-01-2007

[eluser]CodyPChristian[/eluser]
Okay, I read many many docs and rewrote this code many times in the past few hours, but I'm stumped at what the problem may be...

Problem:

I have a function that takes the var's from a form and "updates" a row with them. The row ID comes from the URL. So say I goto "/admin/blog/blog_edit/37" 37 being the id of the post I want to edit. It loads the post and its var's into the form so I can edit them. When I hit submit it send the data from the form to another function named "edit". If I do a print its all working fine but its not updating the row. The problem lies in the ID. I missed something somewhere or something cause its simply not updating the row. Here is the code of the "edit" function.

Code:
function edit()
    {        
        $title = 'title';
        $date = 'date';
        $content = 'content';
        
        $id = $this->uri->segment(4);

        $this->db->where('id', $id);
        $this->db->set('title', $title);
        $this->db->set('date', $date);
        $this->db->set('content', $content);
        $this->db->update('tablename', $_POST);
                
        redirect('admin/blog');
    
    }

I did a simple print_r($id); and it returned a blank page. So something is wrong with the ID. What this may be I'm not sure. I'm sure this is something simple but I'm stumped at this one... Thanks for any help.



*Note: I've also tryed the code below and had the same problems:

Code:
function edit()
    {        
        $title = 'title';
        $date = 'date';
        $content = 'content';
        
        $this->db->where('id', $this->uri->segment(4));
        $this->db->set('title', $title);
        $this->db->set('date', $date);
        $this->db->set('content', $content);
        $this->db->update('tablename', $_POST);
                
        redirect('admin/blog');
    
    }



$id & database issue - El Forum - 07-01-2007

[eluser]Matthew Pennell[/eluser]
Code:
die ($this->uri->segment(4));
If that's empty, it's the action of the form that is wrong. You'd be better off passing the ID of the entry through in the form ($_POST) anyway, rather than putting it in the (hackable) URL. Then you could just use:
Code:
$this->db->where('id', $this->input->post('id'));



$id & database issue - El Forum - 07-02-2007

[eluser]CodyPChristian[/eluser]
Thank you, I don't know why I didn't do that in the first place.. But thanks, everything is now working Smile Cheers.