代码点火器在数据库中插入数组时出错


Codeigniter error inserting array in db

我目前正在制作注册表,您可以在其中批量注册,但出现错误错误号:1054

"

字段列表"中的未知列"test@gmail.com"

插入user (date_registered、test@gmail .com ) 值 (NOW(), '')我的代码是控制器

public function registerbatch(){
        for ($i = 0; $i < count($this->input->post('surname','firstname','age','school','course','email')); $i++) {
            $this->form_validation->set_rules("surname[$i]", "surname[$i]", "required");
            $this->form_validation->set_rules("firstname[$i]", "firstname[$i]", "required");
            $this->form_validation->set_rules("age[$i]", "Age[$i]", "required");
            $this->form_validation->set_rules("school[$i]", "School[$i]", "required");
            $this->form_validation->set_rules("course[$i]", "Course[$i]", "required");
            $this->form_validation->set_rules("email[$i]", "Email[$i]", "required");
        }
        if ($this->form_validation->run() == TRUE) {

      $reg_dat_multi = $this->input->post('surname');
        $reg_dat_multi = $this->input->post('name');
         $reg_dat_multi = $this->input->post('age');
         $reg_dat_multi = $this->input->post('school');
        $reg_dat_multi = $this->input->post('course');
         $reg_dat_multi = $this->input->post('email');
          foreach  ($reg_dat_multi as $reg_dat) {
           $this->user_model->add_batchuser($reg_dat);
        }



         $this->load->view('user/home_view');
        } else {
            $this->load->view('user/batch_register');
        }
    }

供查看

<html>
<head>
</head> 
<body>  
    <form class="form" action="<?php echo base_url() . 'user/registerbatch'; ?>" method="post" class="form-horizontal" role="form">

        <?php for ($i = 0; $i < $num; $i++): ?>
        <br>
        Surname: <input type="text" name="surname[]">
        <br>
        Name: <input type="text" name="firstname[]">
        <br>
        Age:<input type ="int" name ="age[]">
        <br>
        School: <input type="text" readonly value="<?= $school ?>" name="school[]">
        <br>
        Course:<input type ="text" name ="course[]">
        <br>
        Email:<input type ="text" name ="email[]">
        <br>
        <br>

    <?php endfor ?>
   <button type="submit" class="btn btn-success">Register</button>

</body>
</html>

和型号

public function add_batchuser($reg_dat) {
        $this->db->set('date_registered', 'NOW()', FALSE);
        $this->db->insert('user', $reg_dat);
    }

为什么不使用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')

对于您的用例,假设您有相同数量的姓氏、名字和所有输入,您可以分别创建一个数组,然后使用以下insert_batch添加:

public function add_batchuser($reg_dat) {
    foreach ($reg_dat[firstname] as $key => $value) {
        $details[$key]['firstname'] = $reg_dat['firstname'][$key];
        $details[$key]['surname'] = $reg_dat['surname'][$key];
        $details[$key]['age'] = $reg_dat['age'][$key];
        $details[$key]['course'] = $reg_dat['course'][$key];
        $details[$key]['email'] = $reg_dat['email'][$key];
        $details[$key]['date_registered'] => $this->database_now_time();
    }
    $this->db->insert_batch('user', $details);
}

请删除:

foreach  ($reg_dat_multi as $reg_dat) {
    $this->user_model->add_batchuser($reg_dat);
}

您可以只使用:

$reg_dat_multi = $this->input->post(null, true); //This will get all the data in post parameters
$this->user_model->add_batchuser($reg_dat_multi);