简体   繁体   English

插入外键值并提交多行

[英]Insert foreign key value with submit multiple rows

I'm using codeigniter. 我正在使用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. 现在,我想使用此值并将其ID作为外键保存为表。 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. 所以首先第一件事情:你是不是包括下拉以$attributes里面的for循环。

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. 如果您使用的是Codeigniter 3.0,请在此处( http://www.codeigniter.com/userguide3/libraries/form_validation.html#form-validation-tutorial )或在https://ellislab.com/codeigniter/user- guide / libraries / form_validation.html(如果您使用的是旧版本),即使它们或多或少都一样。

Third: Drop the use of $_POST['user'] and use $this->input->post('user'); 第三:删除$_POST['user']使用,并使用$this->input->post('user'); , CodeIgniter will filter the data, if you enabled XSS filtering, and will return false if the value is not set. ,如果启用了XSS过滤,则CodeIgniter将过滤数据,如果未设置该值,则将返回false

Forth: Separate your logic if you're using an MVC framework. 第四:如果您使用的是MVC框架,请分开逻辑。 Move the insert in the model and pass it the data from the controller. 在模型中移动插入物,并将其从控制器传递给数据。

Bonus 奖金

Take a look at $this->db->insert_batch() 看看$ 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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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