Php 如何使用Codeigniter编辑记录
我试图用codeigniter编辑我的db记录,但我做不到,我不明白有什么问题。请帮忙 这是我的控制器代码:Php 如何使用Codeigniter编辑记录,php,codeigniter,codeigniter-2,codeigniter-datamapper,Php,Codeigniter,Codeigniter 2,Codeigniter Datamapper,我试图用codeigniter编辑我的db记录,但我做不到,我不明白有什么问题。请帮忙 这是我的控制器代码: public function edit() { $this->form_validation->set_rules('name','Name','trim|required'); $this->form_validation->set_rules('email','E-mail','trim|required');
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);
}
这是我的型号代码:
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>
身份证件
对于id
,您的输入中有disabled=“disable”
。这意味着它不会包含在表单POST
中。您需要删除disabled=“disable”
试试看:
<label> ID </label>
<input type="text" name="id" id="id" value="<?php echo $record->id ;?>" />
您是否检查了您是否从模型中获取数组$update的值?是的,我检查了iam从数组$update获取更新值。但更新的记录并没有保存到数据库中。如果您在模型中获取值,那么它应该得到更新,因为正如我所看到的,其中没有任何错误。请确保您已通过$this->load->model('test')加载了模型;数组$update的键的名称与字段名相同。检查查询中错误的更好方法是在模型中返回$this->db->last_query();为什么您希望用户更改ID?将其放入type=“hidden”!我不想改变它,我只是想指出,如果有一个禁用的属性,那么它就不会发送。理想情况下,ID应该在URL中。虽然接受的答案有效,但我认为BabyAzerty的答案更合适,因为您不希望有一个表单,允许在发布ID之前更改ID。