Im trying without success to update a row in a table. I mean, when i click on "update" button it goes to another page and i got the previous "values" so i can edit them and finally click on "save".
But it is not working in my program :/..When i click on "edit" button it goes to another page and i only have the previous values of a single field (Field subject). AND when i click on save it does not update anything, it creates a new row.
Here is my code: The index file, here i put the button "edit".
<a href='".site_url('Home/editar')."/$record->id'>
<button type='button' class='btn btn-primary'>EDIT</button></a>
My controller file, with the function "editar" and "saveupdate":
public function editar($id){
$data['carreras'] = $this->Crudmodel->get_carreras();
$data['record']=$this->Crudmodel->get_id_row($id);
$this->load->view('editar',$data);
}
public function saveupdate(){
$id=$this->input->post("txtid");
$txtcarr=$this->input->post("txtcarr");
$txtmat=$this->input->post("txtmat");
$txtdesc=$this->input->post("txtdesc");
$txtcarga=$this->input->post("txtcarga");
$this->Crudmodel->saveup($txtcarr,$txtmat,$txtdesc,$txtcarga);
$this->db->where("id", $id);
redirect('Home/index');
}
The Crudmodel with the function "saveup"
public function saveup($txtcarr, $txtmat, $txtdesc, $txtcarga){
$data=array(
'carrera_id'=>$txtcarr,
'nombre'=>$txtmat,
'descripcion'=>$txtdesc,
'carga_horaria'=>$txtcarga
);
$this->db->update('materias', $data);
}
Here is the get_id_row from crudmodel:
Finally, the "editar" file which contains all the fields.
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h2 align="center">UPDATE SUBJECTS</h2>
<form method="post" action='<?php echo site_url('Home/saveupdate'); ?>'>
<tr>
<td><input type="text" hidden name="txtid" value="<?php echo $record->id ?>"/></td>
</tr>
<tr>
<td>
<select name="txtcarr">
<?php foreach($carreras as $item):?>
<option value="<?php echo $item->id;?>"><?php echo $item->nombre;?></option>
<?php endforeach;?>
</select>
</td>
</tr>
<tr>
<td>Subject : </td>
<td><input type="text" name="txtmat" value="<?php echo $record->nombre ?>"/></td>
</tr>
<tr>
<td>Description : </td>
<td><textarea name="txtdesc" value="<?php echo $record->descripcion ?>"></textarea></td>
</tr>
<tr>
<td>Hours : </td>
<td><input type="text" name="txtcarga"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Save"/></td>
</tr>
<table class="table table-hover" align="center" border="0" cellspacing="0" cellpadding="0" width="300">
</table>
</div>
</div>
</div>
Do not understand what should i do :S