CODEIGNITER how to insert user_ID to mySQL when that user_ID is logged in

心不动则不痛 提交于 2019-12-24 19:36:55

问题


this is the error page

Cannot add or update a child row: a foreign key constraint fails (latihan_ci.adventure, CONSTRAINT adventure_ibfk_1 FOREIGN KEY (user_id) REFERENCES users (user_id) ON UPDATE CASCADE)

INSERT INTO adventure (name, category, place, state) VALUES ('semeru', 'gunung', 'asa', 'asd')

and this is my controller

public function addTrip(){
    $this->load->model('userModel');
    $newTrip = ['name' => $this->input->post('name'),
                'category' => $this->input->post('category'),
                'place' => $this->input->post('place'),
                'state' => $this->input->post('state'),

                ];
            $data['users'] = $this->userModel->getUserId()->result(); 

    $this->db->insert('adventure',$newTrip);
    $this->db->insert('adventure',$data);

    redirect('userController/profile');
}

and this is my Model

public function getUserId()
    {
        return $this->db->get('users',['user_id']);
    }

so how to add the user_ID to the mySQL when the value is user_ID that is already login..Thanks alot


回答1:


when you are login at that time user_id store in session after use

controller

$newTrip = ['name' => $this->input->post('name'),
            'category' => $this->input->post('category'),
            'place' => $this->input->post('place'),
            'state' => $this->input->post('state'),
            'user_id'=>$this->session->userdata('userdata'),
];


来源:https://stackoverflow.com/questions/46430501/codeigniter-how-to-insert-user-id-to-mysql-when-that-user-id-is-logged-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!