我的数据库中有两个表,一个是需求,另一个是users..i我的需求表中有那些用户名,选择框..如何将用户名id存储在数据库中..
这是我的视图:
<div class="form-group">
<label>Choose Vendor</label>
<select class="form-control" multiple class="form-control" data-placeholder="user name" name="user_id[]" >
<option value="0"></option>
<?php foreach($user as $rows) { ?>
<option value="<?php echo $rows->user_id?>"><?php echo ucfirst($rows->first_name)?></option>
<?php } ?>
</select>
</div>
这是我的控制器:
public function requirement()
{
$data["msg"]="";
$this->load->model('RequirementModel');
$data['user']=$this->RequirementModel->getusers();
if($this->input->post())
{
$this->RequirementModel->add_requirement($this->input->post());
redirect(base_url('index.php/Login/dashboard'));
}
$this->load->view('Requirements/requirements',$data);
}
这是我的模型:
function getusers()
{
$this->db->select('*');
$this->db->from('users');
$query = $this->db->get();
//echo $this->db->last_query();
return $query->result();
}
模型 add_requirement :
public function add_requirement($data)
{
$data=array('role_name'=>$post['role_name'],'vacancies'=>$post['vacancies'],'experience'=>$post['experience'],'jd'=>$post['jd'],'hiring_contact_name'=>$post['hiring_contact_name'],'hiring_contact_number'=>$post['hiring_contact_number'],'user_id'=>$user_id);
$this->db->insert('requirements', $data);
}
怎么做?
谢谢
答案 0 :(得分:0)
如下所示更改您的功能add_requirement
并尝试;
function add_requirement($data)
{
if(isset($data['user_id']) && !empty($data['user_id'])) {
$data['user_id'] = implode(',',$user_id);
}
$data=array('role_name'=>$post['role_name'],'vacancies'=>$post['vacancies'],'experience'=>$post['experience'],'jd'=>$post['jd'],'hiring_contact_name'=>$post['hiring_contact_name'],'hiring_contact_number'=>$post['hiring_contact_number']);
$this->db->insert('requirements', $data);
}
}