我尝试更新表中的一行,但没有成功。我的意思是,当我点击“更新”按钮时,它会转到另一个页面,我得到了以前的“值”,这样我就可以编辑它们,最后点击“保存”。
但它在我的程序中不起作用:/..当我单击“编辑”按钮时,它会转到另一个页面,并且我只有单个字段(字段主题)的先前值。当我单击“保存”时,它不会更新任何内容,而是会创建一个新行。
这是我的代码: 索引文件,这里我放了“编辑”按钮。
<a href='".site_url('Home/editar')."/$record->id'>
<button type='button' class='btn btn-primary'>EDIT</button></a>
我的 Controller 文件,具有“editar”和“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');
}
具有“saveup”功能的 Crud 模型
public function saveup($txtcarr, $txtmat, $txtdesc, $txtcarga){
$data=array(
'carrera_id'=>$txtcarr,
'nombre'=>$txtmat,
'descripcion'=>$txtdesc,
'carga_horaria'=>$txtcarga
);
$this->db->update('materias', $data);
}
这是来自 crudmodel 的 get_id_row:
最后是包含所有字段的“editar”文件。
<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>
不明白我应该做什么:S
最佳答案
我认为Rushabh认为您应该将 $id
与数据一起传递给模型,如下所示:
$this->Crudmodel->saveup($txtcarr,$txtmat,$txtdesc,$txtcarga, $id);
在您的模型中,您可以:
$this->db->where("id", $id);
$this->db->update('materias', $data);
并且不要忘记删除 Controller 中的where条件;
编辑:
您的 get_id_row()
函数需要是:
public function get_id_row($id){
$query = $this->db->get_where('your_table_name', array('id' => $id));
return $query->row();
}
关于php - 尝试使用 codeigniter 更新行(表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43144052/