我想将我的 user_ID 插入到基于用户登录的冒险表中。但问题是我从未从用户登录中获取值,该值始终为 NULL
这是错误页面:
Error Number: 1048
Column 'user_id' cannot be null
INSERT INTO
adventure
(name
,category
,place
,state
,user_id
) VALUES ('asd', 'gunung', 'asd', 'asd', NULL)Filename: C:/xampp/htdocs/login-ci/system/database/DB_driver.php
Line Number: 691
这是我的模型
public function getUserId()
{
return $this->session->users['user_id'];
}
这是我的 Controller
public function addTrip(){
$this->load->model('userModel');
$user_id = $this->session->users['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->input->$user_id
];
$this->db->insert('adventure',$newTrip);
// $this->db->insert('adventure',$data);
redirect('userController/profile');
}
请有人告诉我,如果我在代码中遗漏了某些内容,非常感谢
最佳答案
您在创建插入数组时遇到问题。将您的代码更改为:
$user_id = $this->session->users['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' => $user_id // You already have $user_id from session
];
关于php - 当我登录到另一个表CODEIGNITER时如何插入外键ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46439415/