我正在尝试使用codeigniter编辑我的数据库记录,但我做不到,我不明白的问题是什么。请帮忙。
这是我的控制器代码:
public function edit()
{
$this->form_validation->set_rules('name','Name','trim|required');
$this->form_validation->set_rules('email','E-mail','trim|required');
$this->form_validation->set_rules('phone','Phone','trim|required');
if ($this->form_validation->run() == FALSE) {
echo "error";
}
else{
$id = $this->input->post('id');
$this->crud_mdl->editCrud($id);
echo "success";
}
}
这是我的模型代码:
public function editCrud($id)
{
$update = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'phone' => $this->input->post('phone')
);
$this->db->where('id',$id);
return $this->db->update('test', $update);
}
这是我的观点:
<div>
<?php echo form_open('crud/edit');?>
<?php foreach ($records as $record) {?>
<label> ID </label>
<input type="text" name="id" id="id" disabled="disable" value = "<?php echo $record->id ;?>" /><br/>
<label>Name</label>
<input type="text" name="name" id ="name" value = "<?php echo $record->name ;?>" /><br/>
<label>E-Mail</label>
<input type="text" name="email" id ="email" value="<?php echo $record->email ;?>" /><br/>
<label>Phone</label>
<input type="text" name="phone" id ="phone" value = "<?php echo $record->phone ;?>" /><br/>
<input type="submit" name="edit" value="Edit" />
<?php }?>
<?php echo form_close();?>
</div>
您在input
中disabled="disable"
了id
。这意味着它不会包含在表单中 POST
.您需要删除disabled="disable"
。
试试:
<label> ID </label>
<input type="text" name="id" id="id" value="<?php echo $record->id ;?>" />
对于正确的做法,您应该将输入类型设置为 hidden
或将 ID 放在 url 中(取决于您的应用程序)。
<input type="hidden" name="id" id="id" value="<?php echo $record->id ;?>" />