我有一个应用程序,我可以在其中编辑数据库表的字段。一切都很好并且数据已更新,除非我转到我要更新的字段而不是更改文本,而是将该字段留空。这样我得到了 php 错误,因为(我猜)我的控制器的方法丢失了表中该行的 id:
如果有人可以帮助我,我会很高兴,在此先感谢。
我使用 get 方法传递表格行 (edit_fuga.php) 的 id 的视图文件的一部分。
<div id='tab2'>
<?php
$query = $this->db->query("SELECT * FROM fugas WHERE tipo_fuga='2';");
foreach ($query->result_array() as $row)
{
echo "<input type='hidden' name='id_fuga' value='". $row['id_fuga']."'>";
echo $row['desc_fuga']." <a class='button' href='http://localhost/code/index.php/fugas/editar_fuga?var1=".$row['id_fuga']."'>Editar</a>";
echo "<br/>";
}
?>
</div>
我被重定向到的视图(editar_fuga.php):
<?php echo validation_errors(); ?>
<?php echo form_open('fugas/editar_fuga') ?>
<?php
$query = $this->db->query("SELECT desc_fuga_table FROM tipos_fuga where tipo_fuga_table='".$fuga_selected['tipo_fuga']."';");
foreach ($query->result() as $row)
{
echo $row->desc_fuga_table;
}
$tipo_fuga_selected=$row->desc_fuga_table;
?>
<label for="tipo_fuga"><?php echo $tipo_fuga_selected; ?></label><br/>
<p/>
<label for="desc_fuga">Desc da fuga:</label><p/>
<textarea rows="4" cols="50" name="desc_fuga"><?php echo $fuga_selected['desc_fuga']; ?></textarea><p/>
<input type="hidden" name="id_fuga" value="<?php echo $fuga_selected['id_fuga']; ?>">
<input type="submit" name="submit" value="Alterar Fuga" /><p/>
<INPUT Type="button" VALUE="Voltar" onClick="history.go(-1);return true;">
</form>
视图editar_fuga.php的控制器上的方法
public function editar_fuga(){
$fuga_id = $this->input->get('var1');
var_dump($fuga_id);
$this->load->helper('form');
$this->load->library('form_validation');
//$this->load->view('templates/header');
$this->form_validation->set_rules('desc_fuga', 'Descrição da fuga acidental', 'required');
if ($this->form_validation->run() === FALSE)
{
$data['fuga_selected'] = $this->fugas_model->get_fuga($fuga_id);
$this->load->view('fugas/editar_fuga', $data);
}
else
{
$this->fugas_model->update_fuga();
echo "Fuga alterada com sucesso.<br/><br/>";
$this->load->view('fichas/index');
}
}
风雅型号:
public function update_fuga(){
$data = array(
'desc_fuga' => $this->input->post('desc_fuga')
);
$id=$this->input->post('id_fuga');
$this->db->where('id_fuga', $id);
$this->db->update('fugas', $data);
}
public function get_fuga($fuga_id){
var_dump($fuga_id);
$query = $this->db->query("SELECT * FROM fugas where id_fuga='".$fuga_id."';");
foreach ($query->result_array() as $row)
{
$data_fuga = array(
'id_fuga' => $row['id_fuga'],
'tipo_fuga' => $row['tipo_fuga'],
'desc_fuga' => $row['desc_fuga']
);
}
return $data_fuga;
}