fsp712 fsp712 - 24 days ago 13
PHP Question

codeigniter update method not working

I am new to codeigniter and trying to write an update function to update information in my database. I've gone through a few tutorials but for some reason my code is not working! Any tips or assistance would be greatly appreciated. I'm just testing with two fields right now, name and email, and I get a blank page when I go to my update view.

Here is my model method:

function get_by_id($id){
return $this->db->get_where('table',array('id'=>$id));
}

function update($id){
$attributes=array(
'name'=> $this->input->post('Name'),
'email'=> $this->input->post('Email')
);
return $this->db->where('id',$id)
->update('table',$attributes);
}


And here is my relevant controller code:

function edit(){
$data['row']=$this->Mymodel->get_by_id($id)->result();
$data['name']= 'Name';
$data['email']='Email';
$this->load->view('updateview', $data);
}

function update($id){

$this->load->model('Mymodel');
$this->Mymodel->update($id);
if($this->input->post()){
redirect('kittens/view/'.$id, 'refresh');
}


And here is my update view:

<?php echo form_open('kittens/update/')
echo form_hidden('id', $row[0]->id);


foreach($attributes as $field_name){
echo '<p>' . $field_name;
echo form_input($field_name, $row[0]->$field_name) . '</p>';
}
echo form_submit('', 'Update');

echo form_close();
?>


Any help would be appreciated! Not sure what specific part is giving me the trouble!

Answer

Use the following template:

Controller:

public function updateData(){
....
 $data = array(
  "columnName" => $columnValue
 );
 $whereValue = $someNumber;
 $this->model_name->updateTable($data, $wherevalue);
 ...
}    

Model:

public function updateTable($data, $whereValue){
 $this->db->where('columnName', $whereValue);
 $this->db->update('tableName', $data);
}
Comments