我正在尝试使用单个查询向我的数据库插入多行。我看到很多与此有关的问题,但它们对我不起作用。
我能够保存多行但是值都是一样的,它是我输入的最后一个数据所以我想我错过了什么?
以下是代码:
控制器
public function addgrade()
{
$recordid = $this->input->post('recordid');
$studentid = $this->input->post('studentid');
$compid = $this->input->post('compid');
$subcompid = $this->input->post('subcompid');
$grade = $this->input->post('grade');
$classid = $this->input->post('classid');
foreach($this->UserModel->students() as $student):
if ($student->classid==$classid) { //$classid==33
$i=0;
$data = array(
'recordid' =>$recordid,
'studentid' => $studentid,
'compid' => $compid,
'subcompid' =>$subcompid,
'grade' =>$grade
);
$this->Crud->addgrade($data);
}
endforeach;
$this->session->set_flashdata('success', 'Successfully created!');
redirect('instructor');
}
模型
public function addgrade($data)
{
$this->db->insert('grade', $data);
}
查看
<td>
<strong>
<input class="form-control inputScore" type="hidden" name="classid" value="<?php echo $classid;?>"></input>
<input class="form-control inputScore" type="hidden" name="recordid" value="<?php echo $record->id;?>"></input>
<input class="form-control inputScore" type="hidden" name="studentid" value="<?php echo $student->studentid;?>"></input>
<input class="form-control inputScore" type="hidden" name="compid" value="<?php echo $subcomp->id;?>"></input>
<input class="form-control inputScore" type="hidden" name="subcompid" value="<?php echo $subcomp->id;?>"></input>
<input class="form-control inputScore" type="text" name="grade"></input>
</strong></td>
所有给我的是我的数据库中的输出:
答案 0 :(得分:0)
您尝试了这个吗?这里有更详细的信息... Codeigniter - Append rows in view then add those rows to database table
$this->db->insert_batch('grade', $data);
答案 1 :(得分:0)
在表单代码中,对于所有“名称”变量(例如name =“ classid”),请确保在末尾添加“ []” ... name =“ classid []”