CODEIGNITER如何在登录user_ID时将user_ID插入mySQL

时间:2017-09-26 15:30:31

标签: php mysql database codeigniter

这是错误页面

无法添加或更新子行:外键约束失败(latihan_ciadventure,CONSTRAINT adventure_ibfk_1 FOREIGN KEY(user_id)参考usersuser_id)ON UPDATE CASCADE)

INSERT INTO adventurenamecategoryplacestate)VALUES(' semeru',' Gunung',' asa',' asd')

这是我的控制器

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');
}

这是我的模特

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

所以当值为user_ID已经登录时,如何将user_ID添加到mySQL。谢谢

1 个答案:

答案 0 :(得分:1)

当您在登录时user_id使用后在会话中存储

<强>控制器

$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'),
];