I am having a problem understanding what I did wrong in my code for updating data in my database. I'm using CodeIgniter 3.
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 model methods, one for getting the table row, and one for updating:
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 actual update is called:
$this->db->where('id', $id);
$this->db->update('news', $data);