I am new in codeigniter.
I am using codeigniter for this project. I have not getting how to update form data in the database. I have inserting,showing data in the databse is done. But I cant understand, how to update data in the database.
My controller:
JavaScript
x
class User extends CI_Controller {
public function __construct() {
// Call the Model constructor
parent::__construct();
$this->load->model('usermodel');
}
public function insert() {
$this->load->view('userview');
if ($this->input->post('submit')) {
$this->usermodel->save();
}
}
public function display() {
$data = array();
$data['result'] = $this->usermodel->get_contents();
$this->load->view('usergrid', $data);
}
public function edit() {
$data = array();
$get = $this->uri->uri_to_assoc();
$data['result'] = $this->usermodel->entry_update( $get['id'] );
$this->load->view('useredit', $data);
if ($this->input->post('submit')) {
$this->usermodel->entry_update1($get['id']);
}
}
}
model:
JavaScript
<?php
class Usermodel extends CI_Model {
public function __construct() {
// Call the Model constructor
parent::__construct();
}
public function save() {
//print_r($this->input->post('name'));
$data = array(
'name' => $this->input->post('name'),
'age' => $this->input->post('age'),
'address' => $this->input->post('address')
);
//var_dump($this->db);
$this->db->insert('user', $data);
}
public function get_contents() {
$this->db->select('*');
$this->db->from('user');
$query = $this->db->get();
return $result = $query->result();
}
public function entry_update( $id ) {
$this->db->select('*');
$this->db->from('user');
$this->db->where('id',$id );
$query = $this->db->get();
return $result = $query->row_array();
}
public function entry_update1($id) {
$data = array(
'name' => $this->input->post('name'),
'age' => $this->input->post('age'),
'address' => $this->input->post('address')
);
$this->db->where('id', $id);
$this->db->update('user', $data);
}
}
?>
view:
JavaScript
<html>
<head>
<title>user registration</title>
</head>
<body>
<form action="edit" method="POST" name="myform">
<input type="hidden" name="id" value="<?php echo $result['id']; ?>">
username :<input type="text" name="name" value="<?php echo $result['name'] ?>"></br>
age :<input type="text" name="age" value="<?php echo $result['age'] ?>"></br>
Address :<input type="text" name="address" value="<?php echo $result['address'] ?>"></br>
<input type="submit" value="update" name="submit">
</form>
</body>
</html>
Thank you in advance for your help.
Advertisement
Answer
You are only passing $id in
$this->usermodel->entry_update1($get['id']);
and in function u did
JavaScript
public function entry_update1($id) {
$this->db->where('id', $id);
$this->db->update('user', $data);
}
so you have to pass $data also in you function call
$this->usermodel->entry_update1($get['id'], $data);
JavaScript
public function entry_update1($id, $data) {
$this->db->where('id', $id);
$this->db->update('user', $data);
}