我需要从多行插入数据。并且每个行在第三列Class上都有一个数组。我想插入它,使数据库中的数据看起来像下面的MYSQL截图所示。我遇到的错误是类列插入了重复的数据。
。
<script>
var rowCount = 1;
function addMoreRows(frm){
rowCount ++;
var inputer = '<div class="row rowCount'+rowCount+'"><div class="col-md-3"> <div class="form-group"><input type="text" class="form-control" name="subject[]"></div></div><div class="col-md-3"> <div class="form-group"><input type="text" class="form-control" name="highest_mark_obtainable[]"></div></div><div class="col-md-3"><div class="form-group"><select class="form-control" name="class[]"><?php foreach($class_rows as $class_row){echo'<option value="'.$class_row['group'].'">'.$class_row['class'].'</option>';}?></select></div></div><div class="col-md-2"><button class="btn btn-danger" onclick="removeRow('+rowCount+')"><i class="fa fa-minus"></i> Remove row</button></div></div>';
$('.gra-grp-row').append(inputer);
}
function removeRow(removeNum){
$('.rowCount'+removeNum).remove();
}
</script>
Below is my model
public function create_subject(){
$subject = $this->input->post('subject');
$highest_mark_obtainable = $this->input->post('highest_mark_obtainable');
$classes[] = implode(',', $this->input->post('class'));
for($i = 0; $i < count($subject); $i++){
for($p = 0; $p < count($classes); $p++){
$new_subject = array(
'subject' => $subject[$i],
'highest_mark_obtainable' => $highest_mark_obtainable[$i],
'class' => $classes[$p],
'username' => $this->session->userdata('username')
);
$this->db->insert('subject', $new_subject);
}
}
return TRUE;
}
}
Below is my controller
public function create_subject(){
$this->output->enable_profiler(TRUE);
if($this->input->is_ajax_request() && $this->input->post('ajax') == 1){
$this->form_validation->set_rules('subject[]', 'Subject',
'trim|required|min_length[2]|max_length[50]');
$this->form_validation->set_rules('highest_mark_obtainable[]', 'Maximum `enter code here`marks obtainable', 'trim|required|numeric');
$this->form_validation->set_rules('class[]', 'Class', 'trim|required');
if ($this->form_validation->run() == FALSE) {
$this->output->set_status_header('400');
echo '<span class="admin_validation_error" `enter code here`style="color:#ff0000">'.validation_errors().'</span>';
} else {
if($this->subject_model->create_subject() == true){
echo '<span class="validation_success" style="color:green; font- weight:bolder">Well done! Subject(s) successfully created.</span>';
}
}
}else{
redirect('errors/not_found');
}
} `
BELOW IS THE FORM
<div class="row">
<div class="col-md-3">
<div class="form-group">
<label for="Subject">Subject<span class="asterix"> *</span></label>
<input type="text" class="form-control" name="subject[]" id="" placeholder="Mathematics" value="">
</div>
</div><!--Subject-->
<div class="col-md-4">
<div class="form-group">
<label for="Highest mark obtainable">Maximum marks obtainable<span
`class="asterix"> *</span></label>
<input type="text" class="form-control" name="highest_mark_obtainable[]"
id="" placeholder="100" value="">
</div>
</div><!--Highest mark obtainable-->
<div class="col-md-3">
<div class="form-group">
<label for="Class">Classes that do subject<span class="asterix"> *</span>
</label> <select class="form-control select2subjects" name="class[]" ` `multiple="multiple" style="width:100%">
<?php foreach ($class_rows as $class){
echo '<option value="'.$class['class'].'">'.$class['class'].'</option>';
}
?>
</select>
</div>
</div><!--class associated with subject-->
<div class="col-md-2">
</div>
</div>
答案 0 :(得分:1)
我不确定您是否使用Codeigniter 3并且我不确定此功能是否属于Codeginiter 3
您可以在查询构建器$this->db->insert_batch()
$data = array(
array(
'title' => 'My title',
'name' => 'My Name',
'date' => 'My date'
),
array(
'title' => 'Another title',
'name' => 'Another Name',
'date' => 'Another date'
)
);
$this->db->insert_batch('mytable', $data);
// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'), ('Another title', 'Another name', 'Another date')
答案 1 :(得分:0)
{{1}}