Reputation: 31
Hi I faced a problem when I insert some value from dynamic adding table then it insert with cycle.
But I want to insert all my arrays at once how it possible.
This is my Model:
$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);
}
Upvotes: 0
Views: 521
Reputation: 104
$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);
Upvotes: 0
Reputation: 2327
Try this...
$count = $_POST['id'];
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]
);
}
$this->db->insert('workers_tbl',$data);
This may work. Thanks.
Upvotes: -1
Reputation: 235
$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);
Please try this may be work for you....
Put your insert_batch query outside the loop completed. i have only once change and remain same..
Upvotes: 2
Reputation: 1479
You are inserting one record, this isn't called batch insertion. Just create an array and insert the record. In your model function
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);
}
You can secure your input by doing
$this->db->escape($data); // before creating the record array
From your controller just call this function once you store the post in some variable like
public function insert_worker()
{
if($_POST)
{
$data=$this->input->post();
$this->model_name->insert_record($data);
}
}
if you want to create one huge array of multiple records . Read this
Upvotes: 1