【问题标题】:batch insert prevent cyclic insert codeigniter批量插入防止循环插入codeigniter
【发布时间】:2017-03-11 05:05:19
【问题描述】:

您好,当我从动态添加表中插入一些值然后循环插入时,我遇到了一个问题。

但我想一次插入所有数组。

这是我的模特:

$id=$this->input->post('id');
$rank=$this->input->post('rank');
$name=$this->input->post('name');
$mob=$this->input->post('mob');
$count = count($this->input->post('id'));

$data = array();

for($i=0, $i>$count; $i++) {

    $data[] = array(
    'id'=> null,
    'pers_no' => $id[$i],
    'rank' => $rank[$i],
    'name' => $name[$i],
    'mobile' => $mob[$i],
    'vendor_id' => $vendor[$i],
    );

    $result = $this->db->insert_batch('workers_tbl', $data);
}

【问题讨论】:

    标签: php mysql codeigniter


    【解决方案1】:
    $id=$this->input->post('id');
    $rank=$this->input->post('rank');
    $name=$this->input->post('name');
    $mob=$this->input->post('mob');
    $count = count($this->input->post('id'));
    
    $data = array();
    
    for($i=0, $i>$count; $i++) {
    
    $data[] = array(
    'id'=> null,
    'pers_no' => $id[$i],
    'rank' => $rank[$i],
    'name' => $name[$i],
    'mobile' => $mob[$i],
    'vendor_id' => $vendor[$i],
    );}
    
    $result = $this->db->insert_batch('workers_tbl', $data);  
    

    请试试这可能对你有用....

    将您的 insert_batch 查询放在已完成的循环之外。我只有一次改变并保持不变..

    【讨论】:

      【解决方案2】:

      您正在插入一条记录,这不称为批量插入。只需创建一个数组并插入记录。在你的模型函数中

      public function insert_record($data)
      {
         $record=array(
           'pers_no' => $data['id'],
           'rank' => $data['rank'],
           'name' => $data['name'],
           'mobile' => $data['mobile'],
         );
         $this->db->insert('workers_tbl',$record);
      }
      

      您可以通过以下方式保护您的输入

      $this->db->escape($data); // before creating the record array
      

      一旦您将帖子存储在某个变量中,例如

      ,只需从您的控制器调用此函数
      public function insert_worker()
      {
         if($_POST)
         {
           $data=$this->input->post();
           $this->model_name->insert_record($data);
         }
      }
      

      如果你想创建一个包含多个记录的巨大数组。 Read this

      【讨论】:

      • 创建数组数组,阅读我提到的线程的链接
      • 如果它是一个像 5k+ 记录这样的大文件,你可能想在 php.in 或 time_out 中设置 max_exec 时间。有一些默认值会阻止服务器执行长脚本
      【解决方案3】:
      $id=$this->input->post('id');
      $rank=$this->input->post('rank');
      $name=$this->input->post('name');
      $mob=$this->input->post('mob');
      $count = count($this->input->post('id'));
      
      $data = array();
      
      for($i=0, $i>$count; $i++) {
      
          $data[] = array(
          'pers_no' => $id[$i],
          'rank' => $rank[$i],
          'name' => $name[$i],
          'mobile' => $mob[$i],
          'vendor_id' => $vendor[$i],
          );
      
      
      }
      $result = $this->db->insert_batch('workers_tbl', $data);
      

      【讨论】:

      • 你能解释一下你的答案吗?它是如何解决问题的?
      【解决方案4】:

      试试这个...

      $count = $_POST['id'];

      for($i=0, $i

      $data[] = array(
          'pers_no' => $id[$i],
          'rank' => $rank[$i],
          'name' => $name[$i],
          'mobile' => $mob[$i],
          'vendor_id' => $vendor[$i]
      

      );

      }

      $this->db->insert('workers_tbl',$data);

      这可能有效。 谢谢。

      【讨论】:

        猜你喜欢
        • 2013-06-28
        • 2012-01-28
        • 1970-01-01
        • 2015-10-02
        • 2012-06-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多