Hamelraj
Hamelraj

Reputation: 4826

How can i insert multiple arrays into mysql database using codeginiter?

What i want is, i have a uniq id call num i want insert mutiple description for that unique num. i have a form to add fileds dynamically so i can add fields as much i want. when i try to insert one row data its works perfectly when i try to insert more than one row data it doesn't work.

My View page :

<form name="codexworld_frm" action="" method="post">

<div class="field_wrapper">
<input type="text" name="num" value=""/><br />

<input type="text" name="description[]" value=""/><input type="text" name="voucher_no[]" value=""/><input type="text" name="price[]" value=""/>
        <a href="javascript:void(0);" class="add_button" title="Add field"><img src="<?php echo base_url('images/add-icon.png'); ?>"/></a>

</div>
<input type="submit" name="submit" value="SUBMIT"/>
</form>

My Controller :

$data = array(
                'no' => $this->input->post('num'),
                'descriptions' => $this->input->post('description'),
                'voucher' => $this->input->post('voucher_no'),
                'des_price' => $this->input->post('price'),
            );

            $this->multi_model->form_insert($data);

            $data['message'] = 'Data Inserted Successfully';
            //Loading View
            $this->load->view('multi_view', $data);

My Model:

function form_insert($data){

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

if i use foreache loop into my model i think it will work but i how do i use ?

When i use print_r() function this is out put

    1001
Array
    (
        [0] => description1
        [1] => description2
        [2] => description3
    )
    Array
    (
        [0] => voucher 1
        [1] => voucher 2
        [2] => voucher 3
    )
    Array
    (
        [0] => 100
        [1] => 200
        [2] => 300
    )

Upvotes: 0

Views: 5060

Answers (4)

Shahnad S
Shahnad S

Reputation: 1167

you can insert object wise.

in your controller:-

$value=$this->input->post('num');
$count=count($val);
for($i=0; $i<$count; $i++){

$data['no']=$this->input->post('num')[$i];
$data['description']=$this->input->post('description')[$i];
$data['voucher']=$this->input->post('voucher')[$i];
$data['prize']=$this->input->post('prize')[$i];

$this->multi_model->form_insert($data);
}

Upvotes: 1

Nikunj Chotaliya
Nikunj Chotaliya

Reputation: 802

see this link it is use multiple insert without loop or you can use your foreach loop in controller count description post and go through it.

    $data = array();

    $count = count($this->input->post['description']);

    for($i=0; $i < $count; $i++) {
        $data[] = array(
            'no'=>$this->input->post('num'),
            'descriptions' => $this->input->post['descriptions'][$i],
            'voucher' => $this->input->post['voucher'][$i],
            'des_price' => $this->input->post['des_price'][$i],
           );
    }
    $this->db->insert_batch('tbl_description', $data);

Upvotes: 6

shankar kumar
shankar kumar

Reputation: 648

change model as following.

function form_insert($data){
    foreach($data['description'] as $key=>$des)
    {
      $savedata = array(
            'no' => $data('no'),
            'descriptions' => $des,
            'voucher' => $data['voucher'][$key],
            'des_price' => $data['dec_price'][$key],
        );
      $this->db->insert('tbl_description', $savedata);
     }
}

Upvotes: 1

Prasanth Kumar
Prasanth Kumar

Reputation: 33

Hope this will helps you..

Controller

//if voucher_no is required..

$voucher_no = $this->input->post('voucher_no');    
$count = count($voucher_no);
if ($count > 0) {
   for ($i = 0; $i < $count; $i++) {
       if (!empty($voucher_no[$i])) {
          $data = array(
            'no' => $this->input->post('num'),
            'descriptions' => $this->input->post('description')[$i],
            'voucher' => $this->input->post('voucher_no')[$i],
            'des_price' => $this->input->post('price')[$i],
            );

          $this->multi_model->form_insert($data);
        }
      } 
   }

 $data['message'] = 'Data Inserted Successfully';
 //Loading View
 $this->load->view('multi_view', $data);

Let us know the results..

Upvotes: 1

Related Questions