为什么用户没有更新?我正确传递数据吗?

时间:2014-12-22 16:32:32

标签: php codeigniter

我想允许用户更新其帐户详细信息。所以他们填写表格并点击提交。这是代码:

型号:     

public function __construct()
{
    $this->load->database();
}

public function get_details($data)
{
$query = $this->db->get_where('users', array('id' => $data['id']));
return $query->row_array();

}



public function update_user($data)
{

$this->load->helper('url');

$data2 = array(
    'username' => $this->input->post('username'),
    'password' => md5($this->input->post('password')),
    'email' => $this->input->post('email')
    );

$this->db->where('id', $data['id']);
$this->db->update('users', $data2); 

}

}

我的控制器:

public function myaccount()
 {
 $this->load->helper('form');
 $this->load->helper('url');
 $this->load->model('myaccount_model');
 $this->load->library('form_validation'); 
 $config = array(
           array(
                 'field'   => 'username', 
                 'label'   => 'Username', 
                 'rules'   => 'trim|required|min_length[5]|max_length[12]|xss_clean'
              ),
           array(
                 'field'   => 'password', 
                 'label'   => 'Password', 
                 'rules'   => 'trim|required|matches[passconf]'
              ),
            array(
                 'field'   => 'passconf', 
                 'label'   => 'Password Confirmation', 
                 'rules'   => 'trim|required'
              ),
           array(
                 'field'   => 'email', 
                 'label'   => 'Email', 
                 'rules'   => 'trim|required|valid_email'
              )
        );
$this->form_validation->set_message('required', 'Field required');
$this->form_validation->set_rules($config);  
     if($this->session->userdata('logged_in'))

   {
   if ($this->form_validation->run() === FALSE)
   {
   $session_data = $this->session->userdata('logged_in');
   $data['id'] = $session_data['id'];
   $data['result']=$this->myaccount_model->get_details($data);
   $this->load->view('head');
   $this->load->view('myaccount', $data);
   $this->load->view('footer');
   }
 else
 {
    $session_data = $this->session->userdata('logged_in');
 $data['id'] = $session_data['id'];
 $data['result']=$this->myaccount_model->update_user($data);
    redirect ('login', 'refresh');
 }
 } else
 {
 redirect('login', 'refresh');
 }

我认为我没有在此行正确传递数据(用户ID):

$data['result']=$this->myaccount_model->update_user($data);

一切正常,但是当用户点击提交时,页面会刷新,用户数据也不会更新。

谢谢!

2 个答案:

答案 0 :(得分:0)

除非我遗漏了某些内容,否则您的查询(get_details()update_user())都是错误的。

$data只是一个数组,因此您还必须指定id键。

这两个

$data['result']=$this->myaccount_model->get_details($data);
...
$data['result']=$this->myaccount_model->update_user($data);

应该是

$data['result']=$this->myaccount_model->get_details($data['id']);
...
$data['result']=$this->myaccount_model->update_user($data['id']);

答案 1 :(得分:0)

会话中的用户ID可能不正确。确保您的数据在此行正确无误:

$data['id'] = $session_data['id'];

尝试转储$ data和$ data2变量,并在更新之前验证一切正确:

$this->db->update('users', $data2);