Search code examples
phpsqlsql-updatecodeigniter-3insert-update

Codeigniter Update Data In Database


I'm new to Codeigniter and am having a problem understanding what I did wrong in my code for updating data in my database. I would really appreciate your input, since I have been wrecking my brains trying to figure out the problem. Thank you!

This is my controller:

public function edit(){

            $slug=basename($_SERVER['REQUEST_URI']);

            $data['news_item']=$this->news_model->get_news($slug);

            $id=$data['news_item']['id'];


            $this->load->helper('form');
            $this->load->library('form_validation');

            $data['title'] = 'Edit a news item';

            $this->form_validation->set_rules('title', 'Title', 'required');
            $this->form_validation->set_rules('text', 'Text', 'required');

            if ($this->form_validation->run() === FALSE)
                {
                    $this->load->view('templates/header', $data);
                    $this->load->view('news/edit');
                    $this->load->view('templates/footer');

                }
            else{

                $data = array(
                                'title' => $this->input->post('title'),
                                'text' => $this->input->post('text')
                            );

                $this->news_model->edit_news($id,$data); 
                $this->load->view('templates/header', $data);
                $this->load->view('news/success');
                $this->load->view('templates/footer');
                }   

    }

These are my models, one for getting the table row, and one for updating(apparently xD):

public function get_news($slug= FALSE){

        if($slug === FALSE){

            $query=$this->db->get('news');
            return $query->result_array();

        }

        $query=$this->db->get_where('news', array ('slug' => $slug));

        return $query->row_array();

    }

public function edit_news($id,$data){

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

        return;
    }

I have debugged (if you could call it that) the controller and the models, and checked all the important values by printing them or echoing them out, and those are all in order.

Nothing wrong in my opinion with my forms or anything. It all seems to be working fine until the actuall update is called (

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

).

I can't even remember what I tried on this xD Thanks in advance for any input!


Solution

  • Try returning the update

    return $this->db->update('news',$data);
    

    Also where is your input for the id variable?

    maybe do

    $this->db->where('id', $this->input->post('id'))
    

    So it looks like

    $this->db->where('id', $this->input->post('id'));
    return $this->db->update('news',$data);
    

    But just try returning first.

    PART 2 from comments on how to create a update function

    public function edit_news($id,$data){
    
     $slug = url_title($this->input->post('title'));
    
    
      $data = array('title' => $this->input->post('title'),
                  'slug' => $slug,
                  'body' => $this->input->post('body')
    
                    );
    
                  $this->db->where('id', $this->input->post('id'));
    
                  return $this->db->update('posts',$data);
    
    }