Welcome Guest, Not a member yet? Register   Sign In
codeigniter unable to insert multiple row data to database

Hello everyone,

First I'm new here so nice to meet you all,

I had try to insert data using codeigniter like this :

my views :

 <?php echo form_open('index.php/testadddiscount');?>  
    $n = 1;
    foreach($disc as $exist) {
         echo "
           <input name=\"iddisc".$n."\" type=\"text\" value=\"".$iddisc."\" class=\"textbox\" />
           <input name=\"dname".$n."\" value=\"".$discount_name."\" type=\"text\"  />
           } // end foreach

 <input type="submit" value="send" />
<?php echo form_close();?>

my models :

function discount() {
$query = $this->db->get('discount');
$query = $query->result();

$n =1;

foreach($query as $rows) {
  $data[$n] = array(
  'iddiscount'=> $_POST['iddisc'.$n],
  'discountname' => $_POST['dname'.$n],
  'status' => 1

return $this->db->insert_id();

my controller :

function testadddiscount() {



I had try code on above this will show value but unable to insert them into database.

please help me


Any debugging done, did you echo out _POST vars? Any errors, go into index file and change environment to development to show error messages?
Practical guide to IgnitedCMS - Book coming soon

I'm not sure if you copied/pasted your code or just tried to re-create it here, but there are a few problems evident in what you pasted:

In your view, you close the PHP tag after calling form_open(), then proceed to define a variable and run through a foreach loop and echo your inputs without first opening another PHP tag. Initially, I wasn't even sure whether there was an issue with the echoed text, because you used double quotes everywhere and concatenated variables into the echoed text. This is exactly why PHP, JavaScript, and HTML all permit using single or double quotes for strings, avoiding the need for all of the escaped double quotes. Further, PHP's double-quoted strings allow inline variable expansion, making the concatenation unnecessary. I like to enclose my variables in curly braces ({$variable}) to make it a little more clear, and to allow for replacement of variables with class properties or array references if I refactor the code later.

For example:

PHP Code:
echo form_open('index.php/testadddiscount');
   $n 1;
   foreach ($disc as $exist) {
       echo "
    <input name='iddisc
{$n}' type='text' value='{$iddisc}' class='textbox' />
    <input name='dname
{$n}' value='{$discount_name}' type='text' />";
    <input type="submit" value="send" />
echo form_close(); 

At this point, it's also clear that you're setting the values of all of these text inputs to the same two variables, whatever they happen to be.

On to the controller: I generally wouldn't recommend pulling data from $_POST directly (use $this->input->post()), and I would especially recommend not doing this in the model. Get the data in the controller and pass it to the model. If you don't want to pull specific data, you can always use $this->input->post() with no arguments. Further, you're ignoring the result and not doing anything other than calling the model's method, so there would be no indication that anything has happened. In the case of an error, I would usually pass an error message to the view and re-display the view which originally posted the request. If everything went well, I would redirect to a success page or whatever page would make sense.

PHP Code:

class Something extends MY_Controller
   public function testadddiscount() 
$post $this->input->post();
        if (empty(
$post)) {
// do something to indicate that no data was received via POST
        if (
$this->input->post('submit') != 'send') {
// or that the value of the submit button was incorrect

// perform any other controller-level validation/testing of the POST data
        // ...

$numInserted $this->test_model->discount($post);
       if ($numInserted === FALSE) {
           // do something to indicate an error occurred
       } else {
           // do something to indicate success

This points out an issue with the model: you insert some number of rows (if they are inserted successfully), but only indicate the success of the last row inserted, because the call to $this->db->insert_id() is outside of the loop. More than likely, you'd be better off using insert_batch(), which will return the number of rows inserted (or FALSE, 0, or -1 on failure, depending on the driver), and will usually reduce the number of calls to the database. I'm also not sure why you're tying the number of inserts to the number of rows in the discount table, but you could simplify that relationship by using the number of rows if you're not going to actually use the data from the query. Finally, you need some validation of your data, the very least of which is to make sure the data you're trying to use in the $_POST array actually exists before you attempt to insert it. I would recommend doing a little more, but it's dependent on your database structure.

PHP Code:

class SomeModel extends MY_Model
   public function discount($post)
       $query $this->db->get('discount');
       $max $query->num_rows();
       if ($max 1) {
           // error: no data returned from discount table
           return false;

       $numDiscounts range(1$max);
       $data = array();
       foreach ($numDiscounts as $n) {
           if (isset($post["iddisc{$n}"]) && isset($post["dname{$n}"])) {
               $data[] = array(
                   'iddiscount'   => $post["iddisc{$n}"],
                   'discountname' => $post["dname{$n}"],
                   'status'       => 1,

       $numInserted $this->db->insert_batch('test',$data);
$numInserted $numInserted FALSE;


Instead of this code in your view:
PHP Code:
echo "
  <input name=\"iddisc"
.$n."\" type=\"text\" value=\"".$iddisc."\" class=\"textbox\" />
  <input name=\"dname"
.$n."\" value=\"".$discount_name."\" type=\"text\"  />

I recommend:
PHP Code:
echo form_input('iddisc[]',$iddisc[$n],'class="textbox"');
since you are using CI's form helper anyway.
By using square brackets at the end of the input name, $this->input->post('iddisc'); will return an array with all the iddisc input fields.
To process this in your controller (or model), do this:
PHP Code:
$iddiscs $this->input->post('iddisc');
$dnames $this->input->post('dname');

foreach (
$iddiscs as $index->$idd) {
  $data[] = array(
    'iddiscount'   => $idd;
    'discountname' => $dnames[$index],
    'status'       => 1,


Theme © iAndrew 2016 - Forum software by © MyBB