Reputation: 35
I am trying to insert multiple rows to my database using a single query. I saw lots of questions related to this but they dont work for me.
I was able to save the multiple rows but the values are all the same, its the last data that i inputted so i guess i missed something?
Here are the codes:
Controller
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');
}
Model
public function addgrade($data)
{
$this->db->insert('grade', $data);
}
View
<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>
All that gave me is this output on my database:
Upvotes: 0
Views: 493
Reputation: 44
In your form code, for all of your "name" variables such as name="classid", be sure to add "[]" at the end... name="classid[]"
Upvotes: 0
Reputation: 44
Did you try this instead? There's more detailed info about it here... Codeigniter - Append rows in view then add those rows to database table
$this->db->insert_batch('grade', $data);
Upvotes: 0