phpcodeignitertankauth

Codeigniter with tank auth update db table


I have a user profile form that allow user to change their information.

Everything seem to be working fine, except when the form is submitted and profile table got updated but the input fields where to display user information didn't get updated. It still displaying the old information. But if I refresh the page again, it display the right one.

Is there a way to get the new information to display right after submitting?

Here's my code: Tank_auth:

function edit_user_profile($avatar, $gender, $country) 
{
    $user_id = $this->ci->session->userdata('user_id');

    $data = array(
        'avatar'    => $avatar,
        'gender'    => $gender,
        'country'   => $country,
    );

    $this->ci->users->edit_user_profile($user_id, $data);

    return NULL;
}

Controller:

function edit_profile()
{
    if (!$this->tank_auth->is_logged_in()) {                                // not logged in or not activated
        redirect('/login/');

    } else {
        // Get profile data
        $profile = $this->users->get_user_profile_by_id($this->tank_auth->get_user_id());
        $data['avatar_']    = $profile->avatar;
        $data['gender_']    = $profile->gender;
        $data['country_']   = $profile->country;

        $this->form_validation->set_rules('avatar', 'Avatar', 'trim|required');
        $this->form_validation->set_rules('country', 'Country', 'trim|required');
        $this->form_validation->set_rules('gender', 'Gender', 'trim|required');

        $data['errors'] = array();

        if ($this->form_validation->run()) {                            // validation ok
            // Get input fields value
            $avatar = $this->form_validation->set_value('avatar');
            $country = $this->form_validation->set_value('country');
            $gender = $this->form_validation->set_value('gender');

            $this->tank_auth->edit_user_profile($avatar, $gender, $country);

        } else {
            $errors = $this->tank_auth->get_error_message();
            foreach ($errors as $k => $v)   $data['errors'][$k] = '<p>'.$this->lang->line($v).'</p>';
        }

        $this->load->view('auth/edit_profile_form', $data);
    }
}

Model:

function edit_user_profile($user_id, $data)
    {
        $this->db->set('avatar', $data['avatar']);
        $this->db->set('gender', $data['gender']);
        $this->db->set('country', $data['country']);
        $this->db->where('user_id', $user_id);

        $this->db->update($this->profile_table_name, $data);
        return $this->db->affected_rows() > 0;
    }

Solution

  • The reason why you see old data after form submission is the order of operations you perform in controller:

    1. read user data from database and store in $data array
    2. update user data in db (if valid)
    3. show view with data from $data - this is old even if you updated database

    Solution is quite simple - change the order. Move code loading data so it will be after change (2 -> 1 -> 3). You can also update $data variable after update db, depends on you.