I am trying to edit my db record using codeigniter but I can't do it and what's the problem I didn't understand. Please help.
Here is my controller code :
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";
}
}
Here is my model code :
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);
}
Here is my view :
<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>
You have disabled="disable"
in your input
for id
. This means it won't be included in the form POST
. You need to remove disabled="disable"
.
Try that:
<label> ID </label>
<input type="text" name="id" id="id" value="<?php echo $record->id ;?>" />
For proper practices you should set the input type to hidden
instead or put the ID in the url (depends on your application).
<input type="hidden" name="id" id="id" value="<?php echo $record->id ;?>" />