在Codeigniter中将多行插入数据库

时间:2019-02-26 01:13:38

标签: php mysql codeigniter

在我的界面中,我得到了3个常数行,其中连续几行,如Item,Quantity,Weight...。 当我在行中选择数据并提交表单时,仅第3行数据将被插入数据库。 我希望将包含数据的每一行插入数据库。有什么解决办法吗?谢谢。

已更新:: 我曾经尝试使用PHP数组,下面是更新的代码。但是我一直停留在foreach部分,我想将数据插入数据库。

我的观点:

 <tr class="item-details">
                        <td><span class="rowNumber">1</span></td>
                         <td class="">
                        <?php
                        $options = array(
                                         '' => '~Choose An Item~'
                                         );
                        foreach ($item as $rows){
                            $options[$rows->id] = $rows->item_name;
                        }

                        $select = array(
                                        'name' => 'item_name[]',
                                        'id' => 'item_name',
                                        'class' => 'form-control'
                                        );
                        echo form_dropdown('item', $options,set_value('item_name'),$select);
                        ?>
                        </td>
                        <td class=""><input type="number" class="item-qty" name="qty[]"/></td>
                        <td><input type="number" name="weight[]" class="weight" /></td>
                        <td><input type="text" name="promo_code[]" value=""/></td>
            <td><input type="text" name="gp[]" value=""/></td>
                        <td><input type="text" name="discount[]" value=""/></td>
                        <td><input type="text" name="unit_price[]" value=""/></td>
                        <td align="right" class="totalwithouttax">0.00</td>
                        <td align="right" class="totaltax">0.00</td>
                        <td align="right" class="totalamtincltax">0.00</td>
                     </tr><br/>

                     <tr class="item-details">
                        <td><span class="rowNumber">2</span></td>
                         <td class="">
                        <?php
                        $options = array(
                                         '' => '~Choose An Item~'
                                         );
                        foreach ($item as $rows){
                            $options[$rows->id] = $rows->item_name;
                        }

                        $select = array(
                                        'name' => 'item_name[]',
                                        'id' => 'item_name',
                                        'class' => 'form-control'
                                        );
                        echo form_dropdown('item', $options,set_value('item_name'),$select);
                        ?>
                        </td>
                        <td class=""><input type="number" class="item-qty" name="qty[]"/></td>
                        <td><input type="number" name="weight[]" class="weight" /></td>
                        <td><input type="text" name="promo_code[]" value=""/></td>
            <td><input type="text" name="gp[]" value=""/></td>
                        <td><input type="text" name="discount[]" value=""/></td>
                        <td><input type="text" name="unit_price[]" value=""/></td>
                        <td align="right" class="totalwithouttax">0.00</td>
                        <td align="right" class="totaltax">0.00</td>
                        <td align="right" class="totalamtincltax">0.00</td>
                     </tr><br/>

                     <tr class="item-details">
                        <td><span class="rowNumber">3</span></td>
                         <td class="">
                        <?php
                        $options = array(
                                         '' => '~Choose An Item~'
                                         );
                        foreach ($item as $rows){
                            $options[$rows->id] = $rows->item_name;
                        }

                        $select = array(
                                        'name' => 'item_name[]',
                                        'id' => 'item_name',
                                        'class' => 'form-control'
                                        );
                        echo form_dropdown('item', $options,set_value('item_name'),$select);
                        ?>
                        </td>
                        <td class=""><input type="number" class="item-qty" name="qty[]"/></td>
                        <td><input type="number" name="weight[]" class="weight" /></td>
                        <td><input type="text" name="promo_code[]" value=""/></td>
            <td><input type="text" name="gp[]" value=""/></td>
                        <td><input type="text" name="discount[]" value=""/></td>
                        <td><input type="text" name="unit_price[]" value=""/></td>
                        <td align="right" class="totalwithouttax">0.00</td>
                        <td align="right" class="totaltax">0.00</td>
                        <td align="right" class="totalamtincltax">0.00</td>
                     </tr><br/>

控制器:

    //*****************  ORDER ITEM  ********************
        $orderID = 1;
        $productId = $this->input->post('product_id');
        $itemName = $_POST['item_name'];//$this->input->post('item_name');
        $qty = $this-> $_POST['qty'];//input->post('qty');
        $weight = $this-> $_POST['weight'];//input->post('weight');
        $unitPrice =  $_POST['unit_price'];//$this->input->post('unit_price');
        $transportationPrice = $this->input->post('transportation_price');
        $itemType = $this->input->post('item_type');
        $gp = $this-> $_POST['gp'];//input->post('gp');
        $tax = $this->input->post('tax');
        $amtInclTax = $this->input->post('amt_incl_tax');
        $amtWithoutTax = $this->input->post('amt_without_tax');
        $taxCode = $this->input->post('tax_code');

        $orderItems = array (
                                'order_id' =>  $orderHeaderId ,
                                'product_id' =>  7 ,
                                'item' => $itemName ,
                                'qty' =>  $qty ,
                                'weight' => $weight  ,
                                'unit_price' => $unitPrice  ,
                                'transportation_price' => 0  ,
                                'item_type' =>  'GOLD' ,
                                'gp' => $gp,
                                'discount' =>  $discount,
                                'amt_without_tax' => 10,
                                'tax' => 20 ,
                                'amt_incl_tax' => 30,
                                'tax_code' => 40 ,
                             );
        echo '<pre>'; print_r($orderItems); echo '</pre>';
        foreach($itemName as $key => $iN){
            //stuck at here
        }
        $orderHeaderId = $this->order->InsertItemData($orderHeaderId,$orderHeader,$orderItems);

型号:

 public function InsertItemData($orderHeaderId,$orderHeader,$orderItems){
 //take order_id from 'order' table //from InsertData(model) function
 $orderHeaderId = $this->InsertData($orderHeader); 

  // Inserting Order Item
  if($orderItems){
 $this->db->insert_batch('order_items', $orderItems);
  }

}

2 个答案:

答案 0 :(得分:0)

首先使HTML表单输入名称数组的所有3部分都像

 <input type="number" class="item-qty" name="qty[]"/>
 <input type="number" name="weight" class="weight[]" />
 ...

然后将数据数组循环为

$qty = $this->input->post('qty', TRUE);
$weight = $this->input->post('weight', TRUE);
$orderItems = array();
for ($i = 0; $i < count($qty); $i++) {
   if ($qty[$i]) {
      $orderItems[] = array (
                          'order_id' =>  $orderHeaderId ,
                          'product_id' =>  7 ,
                          'item' => 'TEST'  ,
                          'qty' =>  $qty[$i] ,
                          'weight' => $weight[$i]
       ...
       );
   }
 }

现在您可以将insert_batch方法用作

if ($orderItems) {
      $this->db->insert_batch('table_name', $orderItems);
}

答案 1 :(得分:0)

您可以按如下所示修改每个代码。

控制器:

$orderID = 1;
$productId = $this->input->post('product_id');
$itemName = $_POST['item_name'];//$this->input->post('item_name');
$qty = $this-> $_POST['qty'];//input->post('qty');
$weight = $this-> $_POST['weight'];//input->post('weight');
$unitPrice =  $_POST['unit_price'];//$this->input->post('unit_price');
$transportationPrice = $this->input->post('transportation_price');
$itemType = $this->input->post('item_type');
$gp = $this-> $_POST['gp'];//input->post('gp');
$tax = $this->input->post('tax');
$amtInclTax = $this->input->post('amt_incl_tax');
$amtWithoutTax = $this->input->post('amt_without_tax');
$taxCode = $this->input->post('tax_code');

$orderItems = array();
for ($i = 0; $i < count($qty); $i++) {
    if ($qty[$i]) {
        $orderItems[] = array (
            // 'order_id' =>  $orderHeaderId ,
            'product_id' =>  7 ,
            'item' => $itemName[$i] ,
            'qty' =>  $qty[$i] ,
            'weight' => $weight[$i] ,
            'unit_price' => $unitPrice[$i] ,
            'transportation_price' => 0 ,
            'item_type' =>  'GOLD' ,
            'gp' => $gp[$i],
            'discount' =>  $discount[$i],
            'amt_without_tax' => 10,
            'tax' => 20 ,
            'amt_incl_tax' => 30,
            'tax_code' => 40 ,
        );
    }
}
$this->order->InsertItemData($orderHeader,$orderItems);

型号:

 public function InsertItemData($orderHeader,$orderItems) {
    //take order_id from 'order' table //from InsertData(model) function
    $orderHeaderId = $this->InsertData($orderHeader); 

    if($orderItems) {
        // append order_id array into each order item
        foreach ($orderItems as $key => $item) {
            $orderItems[$key] = array('order_id' =>  $orderHeaderId) + $orderItems[$key];
        }

        // Inserting Order Item
        $this->db->insert_batch('order_items', $orderItems);
    }
}