简体   繁体   中英

Insert foreign key value with submit multiple rows

I'm using codeigniter. I have a form which can save multiple data at once. there is a dropdown with data in database. Now I want to take this value and save its id as a foreign key as my table. But it doesn't work.

view

<form class="form-inline" role="form" id="frmadd" action="<?php echo base_url() ?>index.php/boq_controller/create" method="POST">

     <?php
       $attributes = 'class = "form-control" id = "user" name="boq[<?php echo $i ?>][user]"';
        echo form_dropdown('user',$user, set_value('user'), $attributes);?>


        <?php


        $i =0;
        for($i=0;$i<10;$i++){?>
    <tr class="txtMult">


        <td><input type="text" name="boq[<?php echo $i ?>][work_product_id]" class="form-control" id="work_product_id" placeholder=""></td>
        <td><input type="text" name="boq[<?php echo $i ?>][work_item_description]" class="form-control" id="work_item_description" placeholder=""></td>
        <td><input type="text" name="boq[<?php echo $i ?>][quantity]" id="" class="form-control val1" /></td>
        <td><select style=" height: 33px; width: 102px; border-radius: 2px;" name="boq[<?php echo $i ?>][unit]">
            <option value="" selected> </option>
            <option value="cube">cube</option>
            <option value="sq.ft">sq.ft</option>
            <option value="Cwts">Cwts</option>
            <option value="Gal" >Gal</option>
        </select></td>
        <td><input type="text" name="boq[<?php echo $i ?>][rate]" class="form-control val2"/></td>
        <td><input type="text" name="boq[<?php echo $i ?>][laboure_hrs]" id="" class="form-control val3" /></td>
        <td><input type="text"name="boq[<?php echo $i ?>][laboure_cost]" id="" class="form-control val4"/></td>
        <td><input type="text" name="boq[<?php echo $i ?>][others]" class="form-control" id="others" placeholder=""></td>

        <td>
                <span class="multTotal">0.00</span><input type="text" id="txtmultTotal" name="boq[<?php echo $i ?>][txtmultTotal]">
<!--            <input type="text" class="multTotal" placeholder="0.00">-->
        </td>
    </tr>
        <?php }?>

Model

public function create()
{
    foreach($_POST['boq'] as $boq)
    {
        $_POST['user'];  
        $this->db->insert('boq', $boq);
    }
}

controller

public function index()
{  
    $data['user'] = $this-> boq_model ->get_user();
    $this->load->view('admin_include/header');
    $this->load->view('boq/boq',$data);
}

So first things first : you are not including the dropdown with $attributes inside the for loop.

Second: validation . Do you validate you user input? Take a look here ( http://www.codeigniter.com/userguide3/libraries/form_validation.html#form-validation-tutorial ) if you're using Codeigniter 3.0 or here https://ellislab.com/codeigniter/user-guide/libraries/form_validation.html if you're using the old one, even though they should be more or less the same.

Third: Drop the use of $_POST['user'] and use $this->input->post('user'); , CodeIgniter will filter the data, if you enabled XSS filtering, and will return false if the value is not set.

Forth: Separate your logic if you're using an MVC framework. Move the insert in the model and pass it the data from the controller.

Bonus

Take a look at $this->db->insert_batch()

$data = array(
array(
  'title' => 'My title' ,
  'name' => 'My Name' ,
  'date' => 'My date'
),
array(
  'title' => 'Another title' ,
  'name' => 'Another Name' ,
  'date' => 'Another date'
)
);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM