【问题标题】:Codeigniter-- Foreach insert array to DBCodeigniter——Foreach 将数组插入数据库
【发布时间】:2018-03-09 19:41:29
【问题描述】:

所以我想在插入记录时在我的表中实现这一点。

**section       test        sample      quantity    specification**
 molecular    necropsy     Liver            3            n/a
 molecular    necropsy    Trachea           4            n/a
molecular     necropsy    Kidney            5           n/a

注意:

  1. 我只有 1 个 SECTION 下拉菜单和 1 个 TEST 下拉菜单。
  2. 在这 2 个下拉菜单中,我可以有许多样品、数量和规格。

    //This my CONTROLLER
    public function save_section_test1() {
    $section_id = $this->input->post('section');
    $test = $this->input->post('test');
    $samp = $this->input->post('samp');
    $quantity = $this->input->post('quantity');
    $specify = $this->input->post('specify');
    
    $save_sect = array();
    
    for ($i = 0; $i < count($sample); $i++) {
    
        $save_sect[] = array(
        'section_id' => $section_id[$i],
        'test_id' => $test[$i],
        'samp_id' => $sample[$i],
        'quantity' => $quantity[$i],
        'specification' => $specify[$i]
        );
    }
    
    $this->user_model->save_sect1($save_sect);
    redirect(base_url('user/show'));
    }
    
    //This is my MODEL
    public function save_sect1($save_sect) {
    return $this->db->insert_batch('tblsavesection', $save_sect); 
    }
    

谁能帮帮我。非常感谢。

【问题讨论】:

  • 我不明白你能不能简单解释一下,
  • 我只有 1 个 SECTION 输入和 1 个 TEST 输入。但在样品、数量和规格中,我有超过 1 个输入。我想要的是将样品、数量和规格循环到部分和测试。这样我就可以像上面一样得到输出。
  • 您是要优化还是编码,还是遇到了一些错误?
  • 先生,我在使用当前代码时遇到错误。没有任何东西插入到我的数据库中。我的网页一直显示“HTTP ERROR 500”。
  • 你能做一件事吗,只分享echo '&lt;pre&gt;';print_r($_POST);die;放在这行之前$section_id = $this-&gt;input-&gt;post('section');

标签: php sql arrays sql-server codeigniter


【解决方案1】:

根据您的代码,计数是在 $sample 上执行的,但该变量未定义,因此您的 for 循环应该是这样的:

for ($i = 0; $i < count($samp); $i++) {
    $save_sect[] = array(
    'section_id' => $section_id[$i],
    'test_id' => $test[$i],
    'samp_id' => $samp[$i],
    'quantity' => $quantity[$i],
    'specification' => $specify[$i]
    );
}

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    试试这个

    控制器

    public function save_section_test1() {
    
        $post=$this->input->post();
        $result=$this->user_model->save_sect1($post);
    
        redirect(base_url('user/show'));
      }
    

    型号

    public function save_sect1($params) {
        $save_sect = array();
        foreach($params as $key=>$value){
          $save_sect[$key] = array(
              'section_id' => $value['section'],
              'test_id' => $value['test'],
              'samp_id' => $value['samp'],
              'quantity' => $value["quantity"],
              'specification' => $value['specify']
          );
        }
    
        $this->db->insert_batch('tblsavesection', $save_sect);
        return $this->db->trans_status();
      }
    

    【讨论】:

    • 遇到 PHP 错误 严重性:通知消息:未初始化的字符串偏移量:0 文件名:models/user_model.php 行号:153 到 158
    • 请不要说sir,你能分享一下error是哪一行发生的吗?
    • 你能检查一下你的数据库是否插入了 iits
    • 'emp_id' => $value['emp_id'], 'section_id' => $value['section'], 'test_id' => $value['drop_molec'], 'samp_id' => $value['samp'], 'quantity' => $value["quantity"], 'specification' => $value['specify']
    • $save_sect[$key] = array( 在模型下面的行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    • 2015-08-30
    • 2015-08-04
    相关资源
    最近更新 更多