I am trying to update a mySQL table while using CodeIgniter.
The Controller
I have the redirect() commented out to use print_r(). When it returns the array, it returns the correct updated values. But if i uncomment the redirect, then it will redirect me to the page with the table and the values will not get updated. I also check on phpMyAdmin to make sure values arent getting updated and not just displaying, but they arent updating on their either. This is confusing me because the print_r() is returning the correct values.
<?php
class update_ctrl extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('update_model');
}
public function updateGame($id){
$this->load->model('update_model');
$data['games'] = $this->update_model->getGame($id);
$this->load->view('update_view', $data);
$this->load->view('footer');
}
public function update(){
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<span class="error">', '</span>');
$this->form_validation->set_rules('name', 'Name', 'trim|required');
$this->form_validation->set_rules('genre', 'Genre', 'trim|required');
$this->form_validation->set_rules('developer', 'Developer', 'trim|required');
$this->form_validation->set_rules('year', 'YearReleased', 'trim|required|numeric');
$this->form_validation->set_rules('price', 'Price', 'trim|required|numeric');
$id = $this->input->post('ID');
$data = array(
'Name' => $this->input->post('name'),
'Genre' => $this->input->post('genre'),
'Developer' => $this->input->post('developer'),
'YearReleased' => $this->input->post('year'),
'Price' => $this->input->post('price')
);
$this->load->model('update_model');
$this->update_model->update($id, $data);
$this->session->set_flashdata('msg', 'Game Updated!');
print_r($data);
//redirect('');
}
}
?>
The Model
<?php
class update_model extends CI_Model {
public function getGame($id) {
$this->db->select('*');
$this->db->from('games');
$this->db->where('ID', $id);
$query = $this->db->get();
if ($query->num_rows() > 0){
return $query->result();
} else {
return $query->result();
}
}
public function update($id, $data){
$this->db->where('ID', $id);
$this->db->update('games', $data);
}
}
?>