如何处理用户只在页面中更新数据,codeigniter

时间:2015-06-09 07:55:11

标签: php codeigniter

我在codeigniter中开发应用程序时遇到问题。

我有一桌水果:

fid  f_name   f_slug   color
--------------------------
1   apple     apple    red
2   orange    orange    orange

让我们说用户1与苹果相关,用户2与橙色相关。

现在问题是当user1通过访问url“account / edit / apple”或“account / edit / orange”更新apple和orange时,我希望用户1只更新苹果而不是橙色,因为橙色与用户2。

我被困在这里1天,仍然没有解决方案。

帐户控制器中的编辑方法

public function edit($slug)
{
    if (!$this->tank_auth->is_logged_in()) {
        redirect('/auth/login/');
    }else{

        $data['user_id']    = $this->tank_auth->get_user_id();
        $data['username']   = $this->tank_auth->get_username();

        $usern = $data['username'];

        $data['fruits'] =  $this->account_model->get_fruit($slug);

        $data['activeTab'] = "";
        $data['title'] = "Edit fruit details";

        $this->load->view('templates/header', $data);
        $this->load->view('templates/navbar', $data);
        $this->load->view('account/edit', $data);
        $this->load->view('templates/footer');
    }
}

更新方法

function update()
{   
    if (!$this->tank_auth->is_logged_in()) {
        redirect('/auth/login/');
    } else{
        $data = array(

            'f_name' => $this->input->post('Name'),
            'f_color' => $this->input->post('Color'),
            'slug' => $this->input->post('slug'),
        );

        $this->load->model('account_model'); 
        if($this->account_model->update($data))
        {
            echo "successful";
        }
        else
        {
            echo "unsuccess";
        }
    }
}

account_model

public function update($data)
{

    extract($data);
    $this->db->where(array('slug' => $data['slug']));
    $this->db->update('fruits', $data);
    echo $this->db->last_query();
    return true;
}

1 个答案:

答案 0 :(得分:0)

userName.getText().toString()功能中,您还会发送当前登录的用户

注意:假设您将登录的update存储在user's id

session

现在 account_model

function update()
{   
    if (!$this->tank_auth->is_logged_in()) {
        redirect('/auth/login/');
    } else{
        $userId = $this->session->userdata('userId'); //Get the userId
        $data = array(

            'f_name' => $this->input->post('Name'),
            'f_color' => $this->input->post('Color'),

        );

        $this->load->model('account_model'); 
        if($this->account_model->update($data,$userId))
        {
            echo "successful";
        }
        else
        {
            echo "unsuccess";
        }
    }
}

注意:不知道slu from来自哪里。您必须检查,因为您只发送public function update($data,$userId) { extract($data); $this->db->where(array('slug' => $data['slug'],'id' => $userId)); //Note d userId condition too $this->db->update('fruits', $data); echo $this->db->last_query(); return true; } f_name