如何允许用户编辑自己的帐户详细信息


How can i allow a user to edit their own account details?

我如何允许用户编辑自己的帐户详细信息。当他们登录时,将使用他们的用户名创建一个会话。如何查询将会话与数据库中的用户匹配的数据库?我对在哪里做、控制器或模型感到困惑?这是我的代码。它不多,因为我真的不知道从哪里开始:

控制器:

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

型:

<?php
class Myaccount_model extends CI_Model {
public function __construct()
{
    $this->load->database();
}
public function get_details()
{
$query = $this->db->get_where('users', array('id' => $id));
return $query->row_array();
}
}

我收到错误消息:未定义的属性:导航::$myaccount_模型。

我知道这与如何将用户 ID 从控制器传递到模型有关吗?

您需要在使用模型函数之前加载模型

$this->myaccount_model->get_details($data);
$this->load->model('myaccount_model');

$this->load->model('myaccount_model');
$this->myaccount_model->get_details($session_data['id']);

编辑:

如果要从特定 ID(会话 ID(获取详细信息,在模型中的get_details($id)函数中添加参数。

例如:

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

然后在控制器中调用它,如下所示:

$result = $this->myaccount_model->get_details($session_data['id']);

现在将此$result传递给您的视图。

大概您正在会话中设置用户 ID,以便您可以使用

$id = $this->session->get_userdata('id');
return $this->db->get_where('users', array('id' => $id));
 $data['username'] = $session_data['username'];

您传递的不是用户名,而是用户 ID

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