3

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>
Jenz
  • 8,280
  • 7
  • 44
  • 77
Xubayer pantho
  • 329
  • 5
  • 11
  • 28
  • did u checked whether u r getting value for array $update in model?? – Jenz Mar 31 '14 at 07:51
  • yes i checked iam getting updated value from array $update. but updated record is not saved into database. – Xubayer pantho Mar 31 '14 at 07:56
  • If you r getting the values in model, then it should get updated as there is no any error in it as I can see. – Jenz Mar 31 '14 at 08:00
  • make sure that you have loaded the model by $this->load->model('test'); and the keys of array $update have the name same as field name. – Jenz Mar 31 '14 at 08:03
  • Better way to check for error in query is to echo it in model as, return $this->db->last_query(); – Jenz Mar 31 '14 at 08:04
  • if there is no any error why i can't update my record? could you plz check again that i have done everything perfectly. – Xubayer pantho Mar 31 '14 at 08:05
  • Hey, SamV ID actually in POST from a disabled input field, look into my view code. – Xubayer pantho Mar 31 '14 at 08:08

1 Answers1

4

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 ;?>" />
SamV
  • 7,548
  • 4
  • 39
  • 50
  • 2
    Why would you want ID to be changed by the user ? Put it in type="hidden" ! And display it in a span or nothing. – Kalzem Mar 31 '14 at 09:01
  • 1
    I dont want it to be changed, i was just to pointing out that if there is a disabled attribute then it wont send. The ID should be in the URL ideally. – SamV Mar 31 '14 at 09:03
  • 1
    Although the accepted answer works, I think that BabyAzerty's answer is more appropriate as you don't want to have a form where you allow the id to be changed before it's posted. – Donovan Apr 01 '14 at 00:52