这是错误页面
无法添加或更新子行:外键约束失败(latihan_ci
。adventure
,CONSTRAINT adventure_ibfk_1
FOREIGN KEY(user_id
)参考users
(user_id
)ON UPDATE CASCADE)
INSERT INTO adventure
(name
,category
,place
,state
)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。谢谢
答案 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'),
];