user3413834
user3413834

Reputation: 11

cannot update data in codeigniter

I can't update my data in database. Here is my code:

controller :

public function update_contact()
{
    sleep(1);
    $data['validation_error'] = '';
    $this -> load -> library('form_validation');
    $this -> form_validation -> set_rules('name', 'Name', 'required|max_length[40]|callback_alpha_dash_space');
    $this -> form_validation -> set_rules('email', 'Email', 'required|max_length[40]|valid_email');
    $this -> form_validation -> set_rules('phone', 'Phone', 'required|max_length[15]|alpha_numeric');
    $this->form_validation->set_rules('cbo_list', 'Group Name', 'required');

    if ($this -> form_validation -> run() == FALSE) {
        $message = "<strong>Editing</strong> failed!";
        $this -> json_response(FALSE, $message);
    } 
    else {
        $this -> contact_model -> update($this -> input -> post('name'), $this -> input -> post('email'), $this -> input -> post('phone'), $this -> input -> post('cbo_list'), $this -> session -> userdata('uid'));
        $message = "<strong>" . $this -> input -> post('name') . "</strong> has been edited!";
        $this -> json_response(TRUE, $message);
        redirect("site/contacts"); 
    } 
}

and this is my model :

public function update($cid, $name, $email, $phone, $cbo_list)
{
    $this->db->where(array('uid' => $this -> session -> userdata('uid'), 'cid' => $cid))
        ->update('contacts', array('name' => $name, 'email' => $email, 'phone' => $phone, 'gid' => $cbo_list));          
}

for addition info :

my cont s name = site, my model s name = contact_model, my table = contacts and group_contacts where contacts.gid = group_contacts.gid

and you have to mention specific UID (user id) before update your data.

if I run it, there are no error, but I can't modify the data. can anyone of you help me?

Upvotes: 1

Views: 147

Answers (1)

JBC
JBC

Reputation: 112

The parameters are wrong:

$this -> contact_model -> update($this -> input -> post('name'), $this -> input -> post('email'), $this -> input -> post('phone'), $this -> input -> post('cbo_list'), $this -> session -> userdata('uid'));

Move the UID to the first parameter position

$this -> contact_model -> update($this -> session -> userdata('uid'), $this -> input -> post('name'), $this -> input -> post('email'), $this -> input -> post('phone'), $this -> input -> post('cbo_list'));

Alternatively, change your function from:

public function update($cid, $name, $email, $phone, $cbo_list)

to

public function update($name, $email, $phone, $cbo_list, $cid)

Upvotes: 1

Related Questions