Php 批量插入防止循环插入代码点火器

Php 批量插入防止循环插入代码点火器,php,mysql,codeigniter,Php,Mysql,Codeigniter,嗨,我遇到了一个问题,当我从动态加法表中插入一些值,然后它用循环插入 但是我想一次插入我所有的数组 这是我的模型: $id=$this->input->post('id'); $rank=$this->input->post('rank'); $name=$this->input->post('name'); $mob=$this->input->post('mob'); $count = count($this->input->pos

嗨,我遇到了一个问题,当我从动态加法表中插入一些值,然后它用循环插入

但是我想一次插入我所有的数组

这是我的模型:

$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);
}

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

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
在控制器中将post存储在某个变量中后,只需调用此函数,如

public function insert_worker()
{
   if($_POST)
   {
     $data=$this->input->post();
     $this->model_name->insert_record($data);
   }
}

如果您想创建一个包含多个记录的庞大数组

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

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
在控制器中将post存储在某个变量中后,只需调用此函数,如

public function insert_worker()
{
   if($_POST)
   {
     $data=$this->input->post();
     $this->model_name->insert_record($data);
   }
}
如果您想创建一个包含多个记录的庞大数组

试试这个

$count=$_POST['id']

对于($i=0,$i<$count;$i++){

))

}

$this->db->insert('workers\u tbl',$data)

这可能行得通。 谢谢。

试试这个

$count=$_POST['id']

对于($i=0,$i<$count;$i++){

))

}

$this->db->insert('workers\u tbl',$data)

这可能行得通。 谢谢

请尝试这可能是你的工作

将insert\U批处理查询放在已完成的循环之外。我只有一次改变,并且保持不变

$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);
请尝试这可能是你的工作


将insert\U批处理查询放在已完成的循环之外。我只做了一次更改并保持不变。

创建数组数组,阅读我提到的线程链接如果它是一个像5k+记录这样的大文件,您可能需要在php.in或time\u中设置max\u exec time。有一些默认值可以阻止服务器执行长脚本创建数组,读取我提到的线程链接如果它是一个像5k+记录这样的大文件,您可能需要在php.in或time\u out中设置max\u exec time。有一些默认值可以阻止服务器执行长脚本。请解释您的答案?它是如何解决问题的?你能解释一下你的答案吗?它如何解决这个问题?
$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);