【问题标题】:Unable to insert more than 100 rows at a time using codeigniter无法使用 codeigniter 一次插入超过 100 行
【发布时间】:2016-12-14 01:15:15
【问题描述】:

我试图一次插入 100 多条记录。如果有更多记录,它们的表单将不会执行任何操作,除非它会提交并重定向到另一个页面。

如何解决此错误。

我也尝试过使用 insert_batch。但是没有用。 我也更改了 php.ini post_max_size,

请有人帮帮我。

以下是我的控制器和模型代码

控制器代码

foreach($chkproduct as $key=>$chkvalue1){

    foreach($chkvalue1 as $key1=>$chkvalue2 ) {


            $chkvalue=explode("/",$chkvalue2);
            $datachk['product_id']   =$chkvalue[0];
            $datachk['client_id']=$chkvalue[1];
            $ins1=$this->Sub_model->record_count_Product($chkvalue[0]);

            $num1=$ins1->num_rows();

            $qry1=$ins1->row();
            $prodId=$qry1->prod_rand_id;
            $datachk['payment_id']=$paymentid;
            $datachk['prod_rand_id']=$prodId;
            $datachk['sub_type']      =$st1[$chkvalue[0]][$key1];
        $datachk['prod_type']     =$pt1[$chkvalue[0]][$key1];
            $datachk['quantity']    =$qty1[$chkvalue[0]][$key1];

            $datachk['reductionamount'] =$redamount[$chkvalue[0]][$key1];
            $datachk['amountafterreduction'] =$ramount[$chkvalue[0]][$key1];
            $datachk['individual_amt'] =$ramount[$chkvalue[0]][$key1]+$redamount[$chkvalue[0]][$key1];
            $cliname=$clientname[$chkvalue[0]][$key1];
            //$date1=$sd1[$chkvalue];
            //$datachk['start_date']        = date('Y-m-d', strtotime($date1));
            $cliname=$clientname[$chkvalue[0]][$key1];

            $expper=explode("-",$per1[$chkvalue[0]][$key1]);
            //echo $expper[0];
            $smonth=$this->check($expper[0]);
            if($smonth>=10){
                $startyear=$year1[$chkvalue[0]][$key1]-1;
            }else{
                $startyear=$year1[$chkvalue[0]][$key1];
            }
            //echo $st1[$chkvalue];
            if($st1[$chkvalue[0]][$key1]==1){

                $endyear=$year1[$chkvalue[0]][$key1];

            }elseif($st1[$chkvalue[0]][$key1]==2){
                if($smonth>=02  && $smonth<=10){$endyear=$year1[$chkvalue[0]][$key1]+1;}else{$endyear=$year1[$chkvalue[0]][$key1];}
            }elseif($st1[$chkvalue[0]][$key1]==3){
                if($smonth>=02  && $smonth<=10){$endyear=$year1[$chkvalue[0]][$key1]+3;}else{$endyear=$year1[$chkvalue[0]][$key1]+2;}
            }
            //echo $endyear;
            if($smonth==01){$endmonth=12;}else{$endmonth=$smonth-01;}
            $ts = strtotime($expper[0]."".$startyear);
            $lastdate=date('t', $endmonth); 
            if($endmonth=='02'){
                if($endyear%4==0){
                    $lastdate1=29;
                }else{
                    $lastdate1=28;
                }
            }
            elseif($endmonth=='04'  || $endmonth=='06' || $endmonth=='09' || $endmonth=='11'){
                $lastdate1=30;
            }else{
                $lastdate1=31;
            }
            //if($endmonth=='02'){$lastdate1=$lastdate-2;}elseif($endmonth%2==1){$lastdate1=30;}else{$lastdate1=31;}
            $datachk['start_date'] =$startyear."-".$smonth."-01";
            $datachk['end_date'] =$endyear."-".$endmonth."-".$lastdate1;


            $datachk['periodicityno']       = $per1[$chkvalue[0]][$key1];
            $datachk['year']                 = $year1[$chkvalue[0]][$key1];
            $datachk['product_status']     =$ps1[$chkvalue[0]][$key1];

            if($comboval == 1 && $datachk['reductionamount']!=0){
                $datachk['combostatus']=1;
            }else{
                $datachk['combostatus']=0;
            }

            $pbyp1=$this->Sub_model->getProductByperiodicity($chkvalue[0]);
            $datapdf['products'][]=array("pname"=>$pbyp1->productname,"cliname"=>$cliname,"abbr"=>$pbyp1->productshortname,"pername"=>$datachk['periodicityno'],
            "year"=>$datachk['year'],"subtype"=>$st1[$chkvalue[0]][$key1],"perno"=>$pbyp1->periodicity,"dur"=>$pbyp1->duration,"randid"=>$prodId,"prodid"=>$chkvalue[0]);

            $this->Sub_model->addSubscribedProduct($datachk);
            }
        } 

型号

function addSubscribedProduct($data) {

      foreach ($data as $key => $value) {

           if ($value=="") {

                 $array[$key] =0;
           }else{

                 $array[$key] =$value;
            }
      }

  $res = $this->db->insert('iman_subscribed_products', $array);       
//$res = $this->db->insert_batch('iman_subscribed_products', $array);
//echo $sql = $this->db->last_query(); 

         if($res) {
              return 1;
         } else {
              return 0;
         }

}

我的表单截图

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    用户 insert_batch() 代替 insert() 如下:

    $this->db->insert_batch(array)); // 这里数组是多维的,可以包含你需要100s的行数。

    【讨论】:

    • 我试过 $res = $this->db->insert_batch('iman_subscribed_products', $array);但不工作
    • 想要更改代码中的任何内容或我想要更改设置
    • 你添加了$this->db->database();在你的模型中?
    • 不,我没有添加这一行。相同的代码在本地机器上对我有用。不能在我的服务器上工作
    • 好的,您的代码在本地主机上运行,​​但在服务器上不运行。对吗?
    【解决方案2】:

    我认为你偶然发现了 php 的 max_input_vars 选项

    这个的标准值是 1000

    只需将您的价值更改为更高的值

    请注意,您不能使用 ini_set 更改它。 更多信息请点击here

    【讨论】:

    • 感谢您的帮助。更改 max_input_vars 后它的工作。
    猜你喜欢
    • 1970-01-01
    • 2019-01-13
    • 2018-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多