php - 如何插入多行,每行都有一个数组?

标签 php mysql codeigniter

enter image description here enter image description here我需要从多行插入数据。这些行中的每一行在第三列 Class 上都有一个数组。我想插入它,使数据库中的数据看起来像下面的 MYSQL 屏幕截图所示。我遇到的错误是类列插入了重复的数据。 enter image description here .

<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> 

最佳答案

我不确定您是否使用 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')

这里有进一步的解释: https://codeigniter.com/user_guide/database/query_builder.html?highlight=query%20builder#inserting-data

关于php - 如何插入多行,每行都有一个数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42876100/

相关文章:

javascript - MYSQL 更新工作不稳定

php - PHP 文件是否有可能在执行时删除自身?

mysql - 加载每个日期的前 5 条记录

javascript - Codeigniter JQuery 在 jquery .get 请求后动态加载 View 中的数据

mysql - 将查询更改为mysql?

php - 向 MySQL 数据库添加多个字段

PHP pathinfo 被查询字符串中的 url 所迷惑,有什么解决方法吗?

php - 如何使用自动加载 Codeigniter 显示查询计数(*)中的数据?

php - 存储数据的奇怪字符编码,旧脚本显示它们很好,新脚本却没有

php - 代码点火器设计模式