简体   繁体   English

将多行插入数据库codeigniter中的表

[英]inserting multiple rows to table in database codeigniter

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:给我的只是我数据库上的这个输出:

在此处输入图片说明

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这里有关于它的更多详细信息...... Codeigniter - 在视图中附加行,然后将这些行添加到数据库表中

$this->db->insert_batch('grade', $data);

在您的表单代码中,对于所有“名称”变量,例如 name="classid",请务必在末尾添加 "[]"... name="classid[]"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM