1

i am trying to insert multiple rows at once using Codeigniter

my html code for codeigniter view

     <?= form_open(base_url() . 'home/create') ?>
            <tbody>

                <tr>
                    <td class="col-md-1 center-block">
                        <input type="text" name="id[]" value="1" class="form-control" />
                    </td>
                    <td>
                        <input type="text" placeholder="enter descreption" class="form-control" name="desc[]" />
                    </td>
                    <td class="col-md-2 center-block">
                        <input id="space"  onkeyup="sumPrice()" type="number" class="form-control" name="space[]" />
                    </td>
                    <td> 
                        <select onchange="sumPrice()" class="selectpicker price" data-live-search="true" data-width="100%" name="price[]" data-header="Select cost"> 
                            <option value="5">5</option> 
                            <option value="10">10</option>
                            <option value="20">20</option>
                        </select>
                    </td> 
                    <td class="total_price col-md-1">
                        <input type="number" class="form-control" name="total[]" value="1000" />
                    </td> 
                    <td>
                        <button class="btn btn-danger center-block  remove-row"><span class="glyphicon glyphicon-remove"></span> Remove</button>
                    </td>
                </tr>
            </tbody>
        </table>
        <input type="submit" value="insert" class="btn btn-success" />
    </form>
    <button class="btn btn-success append-row"><span class="glyphicon glyphicon-plus-sign"></span> Add new row</button>

and i am using jQuery to insert new row in the table here is js the code

    $('.append-row').click(function() {
    $("table tbody").append
    ('<tr><td class="col-md-1 center-block"><input type="text" name="id[]" value="' + row_id_val++ + '" class="form-control" /></td> <td><input type="text" placeholder="enter descreption" class="form-control" name="desc[]" /></td> <td class="col-md-2 center-block"><input id="space"  onkeyup="sumPrice()" type="number" class="form-control" name="space[]" /></td> <td> <select onchange="sumPrice()" class="selectpicker price" data-live-search="true" data-width="100%" name="price[]" data-header="Select Sub account" onselect="sumPrice()" > <option onselect="sumPrice()" value="5">5</option> <option onselect="sumPrice()" value="10">10</option> <option onselect="sumPrice()" value="20">20</option> </select> </td> <td class="total_price col-md-1"><input type="number" class="form-control" value=""  name="total[]" /></td> <td><button class="btn btn-danger center-block  remove-row"><span class="glyphicon glyphicon-remove"></span> Remove</button></td></tr>');
    $('.selectpicker').selectpicker('refresh');
});

my controller

if ($_POST) {
    $data = array();
    for ($i = 0; $i < count($this->input->post('id')); $i++) {
        $data[$i] = array(
            'row_id' => $this->input->post('id')[$i],
            'desc' => $this->input->post('desc')[$i],
            'space' => $this->input->post('space')[$i],
            'price' => $this->input->post('price')[$i],
            'total' => $this->input->post('total')[$i],
            'code' => time()
        );
    }
    $this->Home_model->create($data);
}

model

function create($data) {
    $this->db->insert_batch('quotation', $data);
}

i tried too many solutions from stackoverflow but none of them worked

4
  • what is your php version?if your php version lower than 5.4 your code will not work Commented Mar 28, 2015 at 11:47
  • @ShaifulIslam PHP Version 5.4.19 Commented Mar 28, 2015 at 11:51
  • your submitting only one array set... how come you expect multiple rows will be inserted.... Commented Mar 28, 2015 at 12:54
  • i am appending new rows when i click on the button @BharathiRaja Commented Mar 28, 2015 at 12:59

3 Answers 3

3

Can you please try below given code? I think there may be issue with $data.

if ($_POST) {
        $data = array();
        for ($i = 0; $i < count($this->input->post('id')); $i++) {
            $data[] = array(
                'row_id' => $this->input->post('id')[$i],
                'desc' => $this->input->post('desc')[$i],
                'space' => $this->input->post('space')[$i],
                'price' => $this->input->post('price')[$i],
                'total' => $this->input->post('total')[$i],
                'code' => time()
            );
        }
        $this->Home_model->create($data);
    }
Sign up to request clarification or add additional context in comments.

7 Comments

owww...getting any error while query execution OR can you diagnose the problem by printing query?
it gives me Message: Undefined offset: 1 also it insert only one row in database and the other rows are empty no data
okay..diagnose it by printing $data value..put die() before $this->Home_model->create($data) statement..there is issue with your data array..also print the $this->input->post('id')[$i] as you have put loop boundary on it.
have you printed the $data and seen does it has required data..... or print the count($this->input->post('id')) and check the count is correct
i have printed echo count($this->input->post('id')) after the loop but the result is 1 although its more than it
|
1

if your php version>=5.4 it should work.
You may try this

    if ($_POST) 
    {
        $row_ids=$this->input->post('id');
        $desc=$this->input->post('desc');
        $spaces=$this->input->post('space');
        $prices=$this->input->post('price');
        $totals=$this->input->post('total');
        $data = array();
        for ($i = 0; $i < count($this->input->post('id')); $i++)
        {
            $data[$i] = array(
                'row_id' => $row_ids[$i],
                'desc' => $desc[$i],
                'space' => $spaces[$i],
                'price' => $prices[$i],
                'total' => $totals[$i],
                'code' => time()
            );
        }
        $this->Home_model->create($data);
    }

4 Comments

can you please print the $data before sending it to model?it seems your data array is not producing properly or you getting some error.If you get any error what error you getting.Write the full error message.
result of printing the array before sending it to model Array ( [0] => Array ( [row_id] => 1 [desc] => test [space] => 10 [price] => 5 [total] => 50 [code] => 1427544624 ) )
something wrong with the array , it comes one time only what's wring ?
if you using this code print out the count($row_ids) and count($this->input->post('id')) what value u getting?if you getting both value 1 then update your question with full view codes
0

Try add enctype="multipart/form-data" in your tag form

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.