我有一个用户个人资料表单,允许用户更改他们的信息。
除非提交表单并且配置文件表已更新,但显示用户信息的输入字段未更新,否则一切似乎都能正常工作。它仍然显示旧信息。但是,如果我再次刷新页面,它会显示正确的页面。
有没有办法在提交后立即显示新信息?
这是我的代码: 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;
}
控制器:
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);
}
}
型号:
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;
}
答案 0 :(得分:2)
您在表单提交后看到旧数据的原因是您在控制器中执行的操作顺序:
解决方案非常简单 - 更改订单。移动代码加载数据,使其在更改后(2 - > 1 - > 3)。你也可以在更新db之后更新$ data变量,取决于你。