我有两个同名的输入 这是我的看法
<input type="text" class="form-control" name="serv[]" >
<input type="text" class="form-control" name="serv[]" >
<input type="hidden" id="dr_id" name="d_id">
我的控制器功能是
public function submit_2()
{
$data = array(
'service_name' => $this->input->post('serv'),
'dr_id' => $this->input->post('d_id'),
);
$this->insert_model->service_insert($data);}
我的模特是
function service_insert($data){
$this->db->insert('services', $data);
}
我收到错误
答案 0 :(得分:1)
$arr = $this->input->post('serv'); //it will contain value in arrray format
foreach($arr as $k=>$v)
{
$data = array(
'service_name' => $v,
'dr_id' => $this->input->post('d_id'),
);
$this->insert_model->service_insert($data);
}
答案 1 :(得分:0)
使用insert_batch()
public function submit_2()
{
$services = $this->input->post('serv');
$data = [];
foreach($services as $service)
{
$data[] = array(
'service_name' => $service,
'dr_id' => $this->input->post('d_id'),
);
}
$this->insert_model->service_insert($data);
}
您的模型
function service_insert($data){
$this->db->insert_batch('services', $data);
}
答案 2 :(得分:-1)
您可以在控制器中使用以下代码
$servData = $this->input->post('serv');
if(is_array($servData))
{
foreach ($servData as $serv)
{
if(!empty($serv))
{
$data[] = array(
'service_name' => $serv,
'dr_id' => $this->input->post('d_id'),
);
}
}
}
$this->insert_model->service_insert($data);