一次为单个外键插入多个记录

based on insert multiple rows using one forigenk value in form

enter image description here

according to above picture there is dropdown for select project.once select project and user will be able to add other details. now i want to do when form is submitting project_id (id map with project name in drop down)should insert with other rows.
but in my case project_id is inserting with only very first row. below result came when i print the array .

Array ( [0] => Array ( [project_id] => 1 [staff_id] => 2 [item_no] => 1 [description] => 1 [qty] => 1 [unit] => cube [rate] => 1 [laboure_hrs] => 1 [laboure_cost] => 1 [amount] => 2 ) 1 => Array ( [project_id] => [staff_id] => 2 [item_no] => 2 [description] => 2 [qty] => 2 [unit] => sq.ft [rate] => 2 [laboure_hrs] => 2 [laboure_cost] => 2 [amount] => 8 ) [2] => Array ( [project_id] => [staff_id] => 2 [item_no] => 3 [description] => 3 [qty] => 3 [unit] => cube [rate] => 3 [laboure_hrs] => 3 [laboure_cost] => 3 [amount] => 18 ) )

how i can pass project_id for another fields. my code is below

controler

public function create(){

//    validate fields
            $this->form_validation->set_rules('work_product_id', 'Work Product Id', 'required');
            $this->form_validation->set_rules('work_item_description', 'Work Item Description', 'required');
            $this->form_validation->set_rules('quantity', 'Quantity', 'required');
           $this->form_validation->set_rules('rate', 'Rate', 'required|numeric');
           $this->form_validation->set_rules('laboure_hrs', 'Laboure Hrs', 'required|numeric');
            $this->form_validation->set_rules('laboure_cost', 'Laboure Cost', 'required|numeric');

           if ($_POST) 
   {
        $project_id=$this->input->post('project');
        $staff_id=$this->input->post('staff_id');
        $item_no=$this->input->post('work_product_id');
        $description=$this->input->post('work_item_description');
        $qty=$this->input->post('quantity');
        $unit=$this->input->post('unit');
        $rate=$this->input->post('rate');
        $laboure_hrs=$this->input->post('laboure_hrs');
        $laboure_cost=$this->input->post('laboure_cost');
        $amount=$this->input->post('txtmultTotal');
           $data= [];

   for ($i = 0; $i < count($this->input->post('work_product_id')); $i++)
    {
       $data[$i] = array(
           'project_id' => $project_id
           'staff_id' => $staff_id[$i],
           'item_no' => $item_no[$i],
           'description' => $description[$i],
           'qty' => $qty[$i],
           'unit' => $unit[$i],
           'rate' => $rate[$i],
           'laboure_hrs' => $laboure_hrs[$i],
           'laboure_cost' => $laboure_cost[$i],
           'amount' => $amount[$i],
        );
       }
        print_r($data);
       $this->boq_model->create($data);
    }      

}

model

function create($data){

     $this->db->insert_batch('boq',$data);

        }

From your html, the project_id is not a control array. So what you should do is to retrieve the project_id before the loop and use it in the loop or remove the [$i] in front of the project_id in the loop

I think you want to insert a single row into your project table, and then insert several rows into your item table using the value of your project_id column as a foreign key.

(With respect, your question is hard to understand; you may get a better answer than mine if you get help editing it from someone with good English-language skills.)

Here's what you do.

First, INSERT a row into your project table. Do not mention the project_id column in your INSERT query, so MySQL will assign an autoincrementing primary key.

  INSERT INTO project (name, whatever) VALUES (?, ?);

Then, retrieve the value of the autoincrementing ID using LAST_INSERT_ID() as follows.

  SET @project_id := LAST_INSERT_ID();

Then, insert rows into your item table using that value.

 INSERT INTO item (project_id, description, qty, whatever)
           VALUES (@project_id, ?, ?, ?)

The trick is to retrieve the value of LAST_INSERT_ID() right after your master-table insertion and store it in the MySQL user-defined variable @project_id. Then you use it for each insertion into your detail table.