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.

5 Answers 5

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);
   }
}
Sign up to request clarification or add additional context in comments.

14 Comments

make sure name of product_rate column is product_rate otherwise change it into controller as u not give rate column name
Do you mean function add_products($data) in model?
I tried your code, but only the first value is inserting into the db.
what is the column name for product rate ? did u get any error?
Column names are okay and I'm not getting any error.
|
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 );
        }
}

3 Comments

Okay, I'll try. Also can you show how to insert the Rate ?
No, Its not working. Only first input value is inserting.
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
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;
      }
  }

Comments

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

1 Comment

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

4 Comments

Where are you calling model in the controller in your answer?
Sorry by mistake i forgot to call model just edited code.
No, its not the problem with the variable name. Its not lopping inside foreach or not getting as array from the form. Do you think am I missing something in View ?
please put this top of the function and share screen short after submit echo '<pre>'; print_r($_POST); exit;

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.