Inserting multiple rows mysql in codeigniter

how to insert multiple array values into database codeigniter
how to insert multiple rows in codeigniter
how to insert multiple records into database using php codeigniter
how to insert multiple values in a single column in mysql using codeigniter
how to insert data using foreach in codeigniter
php codeigniter and jquery ajax append table row using jquery and insert all table data to database
insert batch codeigniter
insert_batch in codeigniter example

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

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

Inserting multiple rows mysql in codeigniter, Codeigniter provides a insert batch function. copytext. Example:- $data = array� SELECT statement can insert as many rows as you want. MySQL INSERT multiple rows example. Let’s take an example of using the INSERT multiple rows statement. First, create a new table called projects for the demonstration:

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

How to insert multiple rows into mysql by a single query using , I understand how to get the query from MySQL, but i don't know how to identify and work with multiple fields from the same type (many sellers). El� Sometime, We may require to insert multiple rows instead of single records. It also takes a few time and adds multiple rows in a database table. Codeigniter 3 have an active records library and they provide insert_batch() to create multiple records at a time. So I also added a small example, so you can see that and learn how it can be possible.

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

How to insert multiple rows from a single view., This video will guide you to insert multiple records into mysql database using PHP Framework Duration: 27:41 Posted: Feb 12, 2017 Best way to Insert multiple rows in a table using PHP/MySQL PHP Code Snippets Iterators multiple rows Insert In this tutorial I will show you ,How to insert multiple rows productively into a table with single SQL Query using PHP's Iterators.

How to Insert Multiple Records into Database using PHP Codeigniter, i have 400000 records in column of one table is it possible to insert all records in another Duration: 10:46 Posted: Jun 11, 2016 6 Comments on Insert multiple rows with CodeIgniter In this post, I will show you how to insert multiple rows in CodeIgniter by using CI’s active records library. CI active record has a function insert_batch().By using this function we can insert multiple rows with a single query and You can either pass an array or an object to the function.

Codeigniter Multiple Insert Part 1, How to insert multiple rows in Codeigniter? In this tutorial, we will learn how to insert multiple records into a MySQL table using CI's active records� Assembling one INSERT statement with multiple rows is much faster in MySQL than one INSERT statement per row.. That said, it sounds like you might be running into string-handling problems in PHP, which is really an algorithm problem, not a language one.

How to insert multiple rows in Codeigniter?, I am trying to insert multiple data in each row., I have a dynamic input which i can add any field I want. here's my controller: $doctorname� To Insert Multiple Rows. If you wish to insert a large number of rows in a table then you can do that too by using the executemany ( ) method. This method has quite a few parameters and the second parameter of this method of insert query in mysql is actually a list of tuples. This list of tuples contains the data that you would want to actually

Comments
  • what is your php version?if your php version lower than 5.4 your code will not work
  • @ShaifulIslam PHP Version 5.4.19
  • your submitting only one array set... how come you expect multiple rows will be inserted....
  • i am appending new rows when i click on the button @BharathiRaja
  • 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
  • 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