在Codeigniter中更新查询

时间:2019-03-25 03:56:54

标签: php codeigniter

我想将数据更新到数据库中,并且必须满足两个条件,才只会更新数据库中的数据。

请查看我的模型,其中有两个条件,即id和order_id。 当仅分配一个条件[ $ this-> db-> where('order_id',$ id); ]时,它对一项有效,但当该订单具有两项以上时,只能更新订单商品最后一行的数据。

所以,我尝试两个条件,即[ $ this-> db-> where('order_id',$ id); ]和[ $ this-> db- > where('id',$ oid); ],但它不起作用。

已更新!

我更改了控制器和模型中的一些代码 我收到一个错误:[数组到字符串的转换],id =数组AND order_id ='10'

控制器:

public function EditOrderPage(){
        $id = $this->uri->segment(3);
        $oid = $this->order->GetCusOrderItemID($id);

        $data['title']       = "JCUBE";
        $data['orders']      = $this->order->GetCusOrder($id);
        $data['orderitems']  = $this->order->GetCusOrderItem($id);
        $data['item']        = $this->order->GetItem();
        $data['projectcode'] = $this->order->GetProjectCode();
        $data['payoption']   = $this->order->GetPayOption();
        $data['eppmonth']    = $this->order->GetEppMonth();
        $this->load->view('ordering/header',$data);
        $this->load->view('ordering/edit_order_page',$data);
        $this->load->view('ordering/footer',$data);

        if($this->input->post('update')){ //if press button 'update order'
            $email       = $this->input->post('email');
            $tel         = $this->input->post('telno');
            $address     = $this->input->post('address');
            $projectCode = $this->input->post('projectcode');
            $payOption   = $this->input->post('options');
            $eppMonth    = $this->input->post('eppmonth');
            $RefOrderID  = $this->input->post('ref_order_id');
            $remark      = $this->input->post('remark');
            $grandTotal  = $this->input->post('grandtotal');
            $this->order->UpdateOrderHeader($email,$tel,$address,$projectCode,$payOption,$eppMonth,$RefOrderID,$remark,$grandTotal,$id);

            $post = $this->input->post();
            $arraySize = count($post['item_id']);       
            for($k=0; $k<$arraySize; $k++){
                $itemName = $this->order->GetItemName($post['item_id'][$k]);

                $productId       = $post['item_id'][$k];
                $itemName        = $itemName['item_name']; //to be search in model using productID
                $quantity        = $post['qty'][$k];
                $weight          = $post['weight'][$k];
                $transportation  = $post['transportation'][$k];
                $premium         = $post['gp'][$k];
                $discount        = $post['discount'][$k];
                $unitPrice       = $post['unit_price'][$k];
                $TotalWithoutTax = $post['totalwithouttax'][$k];
                $TotalTax        = $post['totaltax'][$k];
                $TotalAmtInclTax = $post['totalamtincltax'][$k];
                $this->order->UpdateOrderItems($productId,$itemName,$quantity,$weight,$transportation,$premium,$discount,$unitPrice,$TotalWithoutTax,$TotalTax,$TotalAmtInclTax,$oid,$id);

            }
        }
    }

型号:

public function GetCusOrderItemID($id = NULL){
      $this->db->select('oi.id',$id);
      $this->db->from('order_items oi');
      $this->db->join('order o', 'oi.order_id = o.id');
      $this->db->where('o.id');

      $query = $this->db->get();
      return $query->result_array();
    }

public function UpdateOrderItems($productId,$itemName,$quantity,$weight,$transportation,$premium,$discount,$unitPrice,$TotalWithoutTax,$TotalTax,$TotalAmtInclTax,$oid,$id){
   $OrderItem = array(
                              'product_id'            => $productId,
                              'item'                  => $itemName,
                              'qty'                   => $quantity,
                              'weight'                => $weight,
                              'transportation_price'  => $transportation,
                              'gp'                    => $premium,
                              'discount'              => $discount,
                              'unit_price'            => $unitPrice,
                              'amt_without_tax'       => $TotalWithoutTax,
                              'tax'                   => $TotalTax,
                              'amt_incl_tax'          => $TotalAmtInclTax
                           );
      $this->db->set($OrderItem);
      $this->db->where('id',$oid);
      $this->db->where('order_id',$id);
      $this->db->update('order_items');

查看:

<?php
                foreach($orderitems as $orderitem) { ?>
                <tr class="item-details">
                    <td><input id="orderitems" type="hidden" class="form-control" name="id" value="<?=$orderitem['id']?>" readonly></td>
                    <td class="">
                            <?php
                            $options = array(
                                             '' => '~Choose An Item~'
                                             );
                            foreach ($item as $rows){
                                $options[$rows->id] = $rows->item_name;
                            }

                            $select = array(
                                            'name' => 'item',
                                            'id' => 'item_id',
                                            'class' => 'form-control'
                                            );
                            echo form_dropdown('item_id[]', $options,set_value('item_name',$orderitem['product_id']),$select);
                            ?>
                    </td>
                    <td class=""><input type="number" id="qty[]" class="item-qty" name="qty[]" step="1" min="0" value="<?=$orderitem['qty']?>"/></td>
                    <td><input type="number" name="weight[]" class="weight" step="any" value="<?=$orderitem['weight']?>"/></td>
                    <td><input type="number" name="transportation[]" class="transporation" step="any" value="<?=$orderitem['transportation_price']?>" readonly/></td>
                    <td><input type="text" id="gp[]"  name="gp[]" value="<?=$orderitem['gp']?>" /></td>
                    <td><input type="text" id="discount[]"  name="discount[]" value="<?=$orderitem['discount']?>" readonly/></td>
                    <td><input type="text" id="unit_price[]"  name="unit_price[]" value="<?=$orderitem['unit_price']?>" /></td>
                    <td align="right">
                        <input type="text" id="totalwithouttax[]" name="totalwithouttax[]" value="<?=$orderitem['amt_without_tax']?>" readonly>
                    </td>
                    <td align="right">
                        <input type="text" id="totaltax[]" name="totaltax[]" value="<?=$orderitem['tax']?>" readonly>
                    </td>
                    <td align="right">
                        <input type="text" id="totalamtincltax[]" name="totalamtincltax[]" value="<?=$orderitem['amt_incl_tax']?>" readonly>
                    </td>
                </tr><br/><br><br><br>
                <?php } ?>

我希望我可以在这两个条件下更新项目。希望有人能帮助我。

2 个答案:

答案 0 :(得分:0)

请尝试一下,
我不知道为什么在模型返回中使用结果,但是我改变了两行
型号:

public function GetCusOrderItemID($id = NULL){
      $this->db->select('oi.id as oid');//changes
      $this->db->from('order_items oi');
      $this->db->join('order o', 'oi.order_id = o.id');
      $this->db->where('o.id');

      $query = $this->db->get();
      return $query->row('oid');//changes
    }

答案 1 :(得分:0)

像这样修改GetCusOrderItemID函数:

public function GetCusOrderItemID($id = NULL){
    $this->db->select('oi.id item_id');
    $this->db->from('order_items oi');
    $this->db->join('order o', 'oi.order_id = o.id');
    $this->db->where('o.id',$id);

    $query = $this->db->get();
    return $query->result_array();
}

然后在

中将$oid更改为$oid[$k]['item_id']
$this->order->UpdateOrderItems($productId,$itemName,$quantity,$weight,$transportation,$premium,$discount,$unitPrice,$TotalWithoutTax,$TotalTax,$TotalAmtInclTax,$oid[$k]['item_id'],$id);