dongliangkeng1056 2015-08-29 04:49
浏览 42

通过提交多行插入外键值

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);
}
  • 写回答

1条回答 默认 最新

  • dsc71976 2015-08-29 06:33
    关注

    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); 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值