1

I have a form with below layout in codeigniter:

<input type="text" name="product">`<input type="text" name="cost">`

The form has several rows with the same input names. Have tried several suggestions such as Batch creation and this thread here but not working out

Ayub
  • 25
  • 6

4 Answers4

4
<input type="text" name="product[]">
<input type="text" name="cost[]">

Controller:

function insertData() {
    $product = $this->input->post('product');
    $cost = $this->input->post('cost');

    foreach($product as $key=>$val){

     $data = array(
        'product'     =>$val,
        'cost'  =>$cost[$key]

        );

    }
   $this->db->insert_batch('table_name', $data); 
}
Khant Wai Kyaw
  • 113
  • 1
  • 2
Md. Nashir Uddin
  • 730
  • 7
  • 20
1

Use array of same textboxes name for batch insert

insert_batch function insert multiple data at a time to table in codeigniter

view page

<form action="<?=base_url('Test_c/insert_data')?>" method="post">
            <div class="col-sm-12">
                <input type="text" name="product[]"><input type="text" name="cost[]">
            </div>
            <div class="col-sm-12">
                <input type="text" name="product[]"><input type="text" name="cost[]">
            </div>
            <div class="col-sm-12">
                <input type="text" name="product[]"><input type="text" name="cost[]">
            </div>
            <div class="col-sm-12">
                <input type="text" name="product[]"><input type="text" name="cost[]">
            </div>
            <div class="col-sm-12">
                <input type="text" name="product[]"><input type="text" name="cost[]">
            </div>
            <input type="submit" name="submit" value="submit">
        </form>

controller function

function insert_data() {
            $product = $this->input->post('product');
            $cost = $this->input->post('cost');
            $insert_array = array();
            for ($i=0; $i < count($product); $i++) {
                $tmp = array();
                $tmp['product'] = $product[$i];
                $tmp['cost'] = $cost[$i];
                $insert_array[] = $tmp;
            }

            $this->db->insert_batch('test', $insert_array);
            //echo $this->db->last_query();
    }
Nidhi
  • 1,529
  • 18
  • 28
0

You need to create a post array like so:

<input type="text" name="product[]">
<input type="text" name="cost[]">

Then you can foreach your post and insert_batch or several normal insert();

Callombert
  • 1,099
  • 14
  • 38
  • I have done this...but when I go to post the data, it picks each array as separate i.e. INSERT INTO tablename(product','cost') VALUES(1,2,3,4,5),(120,115,230,105,100) with the first set being proucts and second set being cost...this results to something else. Kindly help – Ayub May 24 '17 at 11:09
0

View

 <input type="text" name="product[]"><input type="text" name="cost[]">

Controller

$arrayOne = $this->input->post('product'); //array(1,2,4);
$arrayTwo = $this->input->post('cost'); //array(22,44,55);
$c = array_combine($arrayOne,$arrayTwo ); // output:array(1=>22,2=>44,4=>55)

foreach($c as $key=>$val){

  $data = array(
     'product_id' => $key ,
     'cost' => $val 
  );

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

}
Naim Malek
  • 1,186
  • 1
  • 11
  • 21