3

I just want to insert dynamic generated input field data into database . My db table having three fields , id(Auto Increment), product_name and rate . I'm trying to insert bulk data into database using dynamically generated input fields where I can add/remove input fields manually. enter image description here I created the input fields as

<input class="form-control" placeholder="Product Name" name="prodname[]" type="text">
<input class="form-control" placeholder="Product Rate" name="prodrate[]" type="text">

This is my controller below

function Act_AddProducts() {
        if ( $this->input->post( 'prodname' )&&$this->input->post( 'prodrate' )) {
            foreach ( $this->input->post( 'prodname' ) as $key => $value ) {

                $this->ProductModel->add_products( $value );
            }

        }

Model function is below

function add_products($val)
  {
      if($this->db->insert('tbl_product_master', array('product_name' => $val)))
      {
        return true;
      }
      else
      {
        return false;
      }
  }

Now the value is inserting into db but one at a time. So please help me to identify the issue with code. Also I don't really understand how to insert prodrate[] value into the same insert query.

Pradeep
  • 9,667
  • 13
  • 27
  • 34
Sudhi Sr
  • 139
  • 1
  • 2
  • 10

5 Answers5

4

Hope this will help you

Your controller Act_AddProducts should be like this :

function Act_AddProducts() 
{
   $prodnames = $this->input->post( 'prodname' );
   $prodrates = $this->input->post( 'rate' );
    if ( ! empty($prodnames) && ! empty($prodrates) ) 
    {
        foreach ($prodnames as $key => $value ) 
        {
            $data['product_name'] = $value;
            /* make sure product_rate columns is correct i just guess it*/
            $data['product_rate'] = $prodrates[$key];
            $this->ProductModel->add_products($data);
        }

    } 
}

Your model add_products should be like this :

function add_products($data)
{
   if ( ! empty($data))
   {
      $this->db->insert('tbl_product_master', $data);
   }
}
Pradeep
  • 9,667
  • 13
  • 27
  • 34
0

just pass input value to the model as it is, then use foreach inside model

function add_products($val)
{
   foreach ( $val as $key => $value ) {

            $this->db->insert('tbl_product_master', array('product_name' => $value );
        }
}
Bergin
  • 188
  • 3
  • 12
  • Okay, I'll try. Also can you show how to insert the **Rate** ? – Sudhi Sr Jul 13 '18 at 06:53
  • No, Its not working. Only first input value is inserting. – Sudhi Sr Jul 13 '18 at 06:58
  • don't use return insert foreach in model... even after removing model, only one data is inserting... I can provide a sample code... But in this code instead of model I made everything in controller... just go through that – Bergin Jul 13 '18 at 07:47
0

TRY THIS

controller

function Act_AddProducts() {
$product_rate = $data = array();

$product_rate = $this->input->post( 'prodrate' );
$product_name = $this->input->post( 'prodname' )
        if ( !empty($this->input->post( 'prodname' ))&&!empty($this->input->post( 'prodrate' ))) {
            foreach ( $product_name as $key => $value ) {
            $data['product_name'] = $value;
            $data['product_rate'] = $product_rate[$key];
                $this->ProductModel->add_products($data);
            }

        }

model

      function add_products($data)
  {
        $product_name = $data['product_name'];
        $product_rate = $data['product_rate'];
      if($this->db->insert('tbl_product_master', array('product_name' => $product_name,'product_rate' => $product_rate)))
      {
        return true;
      }
      else
      {
        return false;
      }
  }
vijay
  • 39
  • 6
0

This is just for your reference.... A simple sample code for dynamic insert.

defined('BASEPATH') OR exit('No direct script access allowed');
class Checking extends CI_Controller {
public function index()
{ 
echo "<form method='post' action='". base_url("Checking/save") ."'>";
    for($i=0;$i<=5;$i++)
    {
            echo "<input type='text' name='input_text[]'>";
    }
    echo "<button type='submit'>Submit</button></form>";
}

public function save(){
    foreach($this->input->post("input_text") as $Row){
        $this->db->insert("checking",array("input_text"=>$Row['input_text']));
    }
}
}

create a controller as Checking.php, and run this .. you will get idea

For database

CREATE TABLE `checking` (
`ch` int(11) NOT NULL AUTO_INCREMENT,
`input_text` varchar(255) DEFAULT NULL,
PRIMARY KEY (`ch`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8
Bergin
  • 188
  • 3
  • 12
  • For immediate purpose I did everything in controller, Just take this as a sample to get some idea as you are a fresher. Do the insert section inside the model – Bergin Jul 13 '18 at 07:51
0

If you want upload bulk record then use insert_batch instead of simple insert your Controller should be

function Act_AddProducts() 
{
$product_rate = $_POST['prodrate'];
$product_name = $_POST['prodname'];

if(!empty($product_rate) && !empty($product_rate)){
$data_array = array();
  foreach ($product_rate as $key => $value ) 
        { 
            $tmp_array = array();
            $tmp_array['product_name'] = $value;
            $tmp_array['product_rate'] = $product_rate[$key];
      $data_array[] = $tmp_array;
        }
   $this->ProductModel->add_products($data_array);
}

model should be

function add_products($data)
  {


      if($this->db->insert_batch('tbl_product_master', $data))
      {
        return true;
      }
      else
      {
        return false;
      }
  }