【问题标题】:empty field show error for insert batch in codeigniter在 codeigniter 中插入批处理的空字段显示错误
【发布时间】:2014-09-18 05:16:33
【问题描述】:

我有多个表单域。我想在 codeigniter 中使用 insert_batch 在 db 中插入超过 25 行。喜欢

 public function purchase(){

  $data = array(

  array(

         'date'=> $this->input->post('date'),
         'vendor_name'=>$this->input->post('vendor_name'),
         'model'=>$this->input->post('model'),
         'price' =>$this->input->post('price'), 
         'purchase_quantity'=>$this->input->post('quantity'),
         'amount'  =>$this->input->post('price')*$this->input->post('quantity'),
         'invoice_no'=>$this->input->post('invoice')
         ),

          array(
         'date'=> $this->input->post('date'),
         'vendor_name'=>$this->input->post('vendor_name2'),
         'model'=>$this->input->post('model2'),
         'price' =>$this->input->post('price2'), 
         'purchase_quantity'=>$this->input->post('quantity2'),
         'amount'  =>$this->input->post('price2')*$this->input->post('quantity2'),
         'invoice_no'=>$this->input->post('invoice')
         ),

          array(
         'date'=> $this->input->post('date'),
         'vendor_name'=>$this->input->post('vendor_name3'),
         'model'=>$this->input->post('model3'),
         'price' =>$this->input->post('price3'), 
         'purchase_quantity'=>$this->input->post('quantity3'),
         'amount'  =>$this->input->post('price3')*$this->input->post('quantity3'),
         'invoice_no'=>$this->input->post('invoice')
         )

         );

  $insert = $this->db->insert_batch('purchase',$data);

  return $insert; 

  }

但是如果我完成两个字段并提交它会显示错误,例如

不正确的整数值:第 3 行的列 'purchase_quantity' 的 ''

INSERT INTO `purchase` (`amount`, `date`, `invoice_no`, `model`, `price`, 

`purchase_quantity`, `vendor_name`) VALUES 

(4995,'18-09-2014','vvvvv','m6','999','5','mizan'), 

(5000,'18-09-2014','vvvvv','ab12','1000','5','abcde'), (0,'18-09-2014','vvvvv',0,0,'','')

文件名:C:\xampp\htdocs\final\sys\database\DB_driver.php

行号:330

请帮忙。

【问题讨论】:

  • 检查条件($this->input->post('quantity3'))?$this->input->post('quantity3'):0
  • 试试这样'purchase_quantity'=>(int)$this->input->post('quantity3'),
  • 亲爱的先生。 Kumar_v3,错误消失了。谢谢。但实际上我想插入那些我填写的字段。而其他人则想忽略。但在数据库中插入日期和发票等 0。我想完全忽略。
  • 请帮忙。再次感谢您的帮助
  • 亲爱的先生。 Senk,错误消失了。谢谢。但实际上我想插入那些我填写的字段。而其他人则想忽略。但在数据库中插入日期和发票等 0。我想完全忽略。

标签: php mysql codeigniter


【解决方案1】:

在整数值之前使用 intval..

intval($this->input->post('quantity2'))

https://ellislab.com/forums/viewthread/238144/

【讨论】:

    【解决方案2】:

    如果使用变量插入批量数据会更好。

    $purchase_quantity=empty($this->input->post('quantity3'))==true?0:$this->input->post('quantity3');
    

    并在数组中使用这个$purchase_quantity 来插入。

    上面的代码将检查 quantitiy3 是否有空值(即 0、null 或 false),它将替换为 0(整数)并保存在变量中,您可以在数组中使用它来插入

    【讨论】:

      【解决方案3】:

      如果表单元素像这样为空,您可以将值设置为 null 或默认值..

      'date'=>($this->input->post('date'))?$this->input->post('date'):null;
      

      或

      'date'=>($this->input->post('date'))?$this->input->post('date'):date('Y-m-d');
      

      如果你想完全忽略插入,也可以这样做:

      $data = array();
      for($i=1;$i<=25;$i++)
      {
          if($i>1)
          {
            if($this->input->post('vendor_name'.$i) && $this->input->post('model'.$i) && $this->input->post('price'.$i) && $this->input->post('quantity'.$i))
            {
              $record = array(
               'date'=> $this->input->post('date'),
               'vendor_name'=>$this->input->post('vendor_name'.$i),
               'model'=>$this->input->post('model'.$i),
               'price' =>$this->input->post('price'.$i), 
               'purchase_quantity'=>$this->input->post('quantity'.$i),
               'amount'  =>$this->input->post('price'.$i)*$this->input->post('quantity'.$i),
               'invoice_no'=>$this->input->post('invoice')
              );
              $data[]=$record;
            }
          }
          else{
             if($this->input->post('vendor_name') && $this->input->post('model') && $this->input->post('price') && $this->input->post('quantity'))
            {
               $record = array(
               'date'=> $this->input->post('date'),
               'vendor_name'=>$this->input->post('vendor_name'),
               'model'=>$this->input->post('model'),
               'price' =>$this->input->post('price'), 
               'purchase_quantity'=>$this->input->post('quantity'),
               'amount'  =>$this->input->post('price')*$this->input->post('quantity'),
               'invoice_no'=>$this->input->post('invoice')
              );
              $data[]=$record;
            }
          }
      }
      

      【讨论】:

      • 我得到一个错误列'amount'不能为空插入到purchase(amount,date,invoice_no,model,price,purchase_quantity,@ 987654331@) 值 (4995,'18-09-2014','tttttt','m6','999','5','mizan'), (4500,'18-09-2014','tttttt' ,'cc32','900','5','mizan'), (NULL,'18-09-2014','tttttt',NULL,NULL,NULL,NULL)
      • 表中的列需要允许空值
      • 把$data[]=$record;如果在两种情况下都在内部
      猜你喜欢
      • 2017-04-01
      • 1970-01-01
      • 2017-08-19
      • 2016-03-19
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多