插入批处理codelgniter'字段列表'中的未知列'数组'

时间:2018-02-09 07:25:58

标签: codeigniter

我是CI的初学者。我在CodeIgniter中的insert_batch函数中收到错误。当我将数组插入insert_batch时,我收到此错误

“字段列表”中的未知列“数组”  和 数组到字符串转换  我做了很多解决方案,但仍然遇到此错误, 谁能给我一个想法?

提前谢谢

我的观点中

@IBInspectable var setImageName: String {
        get{
            return ""
        }
        set{
            let containerView: UIView = UIView(frame: CGRect(x: 0, y: 0, width:50, height: self.frame.height))
            let imageView: UIImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
            imageView.image = UIImage(named: newValue)!
            containerView.addSubview(imageView)
            imageView.center = containerView.center
            self.rightView = containerView
            self.rightViewMode = UITextFieldViewMode.always
        }
    }

控制器

<input type="text" name="companionship[]"> and so forth.

模型功能

public function addstatistics()
{   
     $i =0;       
     foreach($_POST['companionship_id'] as $companionship_id):
        $value1[$i++] = array(
            'companionship_id'=>    $companionship_id
        );
     endforeach;

     foreach($_POST['zone_id'] as $zone_id):
            $value2[$i++] = array(
                    'zone_id'=> $zone_id
            );
     endforeach;

     foreach($_POST['district_id'] as $district_id):
            $value3[$i++] = array(
                'district_id'=> $district_id
            );
     endforeach;

     foreach($_POST['area_id'] as $area_id):
            $value4[$i++] = array(
                'area_id'=> $area_id
            );
     endforeach;

     foreach($_POST['baptism'] as $baptism):
            $value5[$i++] = array(
                'baptism'=> $baptism
            );
     endforeach;

     foreach($_POST['confirm'] as $confirm):
            $value6[$i++] = array(
                'confirm'=> $confirm
            );
     endforeach;

     foreach($_POST['ibd'] as $ibd):
            $value7[$i++] = array(
                'ibd'=> $ibd
            );
     endforeach;

     foreach($_POST['iasm'] as $iasm):
            $value8[$i++] = array(
                'iasm'=>$iasm
            );
     endforeach;

     foreach($_POST['ni'] as $ni):
            $value9[$i++] = array(
                'ni'=>$ni
            );
     endforeach;

     foreach($_POST['ph'] as $ph):
            $value10[$i++] = array(
                'ph'=>$ph
            );
     endforeach;

     foreach($_POST['wh'] as $wh):
        $value11[$i++] = array(
            'wh'=>$wh
        );
     endforeach;

   $this->my_model->addstatistics($value1,$value2,$value3,$value4, $value5,$value6,$value7,$value8,$value9,$value10,$value11);
 }   

有人能告诉我如何解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

试试这个。

public function addstatistics($value1,$value2,$value3,$value4,$value5, $value6,$value7,$value8,$value9,$value10,$value11)
{
      $data = array(
                'companionship_id'  => $value1,
                'zone_id'           => $value2,
                'district_id'       => $value3,
                'area_id'           => $value4,
                'baptism'           => $value5,
                'confirm'           => $value6,
                'ibd'               => $value7,
                'iasm'              => $value8,
                'ni'                => $value9,
                'ph'                => $value10,
                'wh'                => $value11

           );

           $row = array();
           $columns = array();
           for($x=0; $x<count($data); $x++)
           {
                $row = array(
                    'companionship_id'=> $data['companionship_id'][$x]['companionship_id'],
                    'zone_id'=> $data['zone_id'][$x]['zone_id'],
                    'district_id'=> $data['district_id'][$x]['district_id'],
                    'area_id'=> $data['area_id'][$x]['area_id'],
                    'baptism'=> $data['baptism'][$x]['baptism'],
                    'confirm'=> $data['confirm'][$x]['confirm'],
                    'ibd'=> $data['ibd'][$x]['ibd'],
                    'iasm'=> $data['iasm'][$x]['iasm'],
                    'ni'=> $data['ni'][$x]['ni'],
                    'ph'=> $data['ph'][$x]['ph'],
                    'wh'=> $data['wh'][$x]['wh'],
                    'year'=> date('Y'),
                    'month'=> date('M'),
                    'week' => weekdate(),
                 'created_by'=> $this->session->userdata('login_id')

           );
           array_push($columns, $row);
           $rows = array();
         }
          //printA($columns);

         $query= $this->db->insert_batch('monthly_statistics', $columns);
 }

答案 1 :(得分:0)

经过几个小时的思考,我已经修复了错误 现在它工作正常

这是代码

public function mymethod()
    {



        $companionship_id = $this->input->post('companionship_id[]');
        $zone_id = $this->input->post('zone_id[]');
        $district_id = $this->input->post('district_id[]');
        $area_id = $this->input->post('area_id[]');
        $baptism = $this->input->post('baptism[]');
        $confirm = $this->input->post('confirm[]');
        $ibd = $this->input->post('ibd[]');
        $iasm = $this->input->post('iasm[]');
        $ni = $this->input->post('ni[]');
        $ph = $this->input->post('ph[]');
        $wh = $this->input->post('wh[]');

        $value = array();
        for($i=0; $i<count($companionship_id); $i++)
        {
            $value[$i] = array(
                'companionship_id'  =>      $companionship_id[$i],
                'zone_id'           =>      $zone_id[$i],
                'district_id'       =>      $district_id[$i],
                'area_id'           =>      $area_id[$i],
                'baptism'           =>      $baptism[$i],
                'confirm'           =>      $confirm[$i],
                'ibd'               =>      $ibd[$i],
                'iasm'              =>      $iasm[$i],
                'ni'                =>      $ni[$i],
                'ph'                =>      $ph[$i],
                'wh'                =>      $wh[$i]

            );
        }

        $this->db->insert_batch('monthly_statistics',$value);


        $this->session->set_flashdata("success",alert("alert-success","Successfully Inserted"));
        redirect(base_url('to_url'));
        exit();
    }