在codeigniter中插入新记录并同时更新

时间:2015-01-23 06:46:56

标签: php codeigniter session

这是我保存和更新采购订单的代码。

  function save(&$purchase_data,$purchase_id)
    {
        $success=false;

        //Run these queries as a transaction, we want to make sure we do all or nothing
        $this->db->trans_start();

        if($purchase_data)
        {
            if (!$purchase_id or !$this->exists($purchase_id))
            {  
                //$purchase_data['purchase_id'] = $purchase_id = $purchase_data['purchase_id'];
                $success = $this->db->insert('purchase_order',$purchase_data);
                $post_array['cart']=$this->session->userdata('data');
                /*print_r($post_array);
                exit;*/
                $purchase_id=$this->db->insert_id();
                $i=0;
    foreach($post_array['cart'] as $item)
    {
        //echo "<pre>"; print_r($item); echo "</pre>";
        $query = $this->db->query("SELECT name FROM phppos_items WHERE item_id='".$item['product_id']."'");
        foreach ($query->result() as $row)
{
    $product_name=$row->name;

}
        $product_id=$item['product_id'];
        $quantity=$item['quantity'];
        $unit=$item['unit'];
        $unit_rate=$item['unit_rate'];
        $query = $this->db->query("insert into phppos_productdetails(product_id,product_name,quantity,unit,unit_rate,purchase_id) values ('$product_id','$product_name','$quantity','$unit','$unit_rate','$purchase_id')");

        $i++;
    }
            }
            else
            {

                $this->db->where('purchase_id', $purchase_id);
                $success = $this->db->update('purchase_order',$purchase_data);
                //$this->session->set_userdata('sess_products');
                $post_array['cart']=$this->session->userdata('sess_products');

                $i=0;
    foreach($post_array['cart'] as $item)
    {
        echo "<pre>"; print_r($item); echo "</pre>";

        $query = $this->db->query("SELECT name FROM phppos_items WHERE item_id='".$item['product_id']."'");
        foreach ($query->result() as $row)
{
    $product_name=$row->name;

}

        $product_id=$item['product_id'];
        $quantity=$item['quantity'];
        $unit=$item['unit'];
        $unit_rate=$item['unit_rate'];

        $query = $this->db->query("update phppos_productdetails set product_id='$product_id',product_name='$product_name',quantity='$quantity',unit='$unit',unit_rate='$unit_rate' where purchase_id='$purchase_id'");

        $i++;
    }   

            }




        }

        $this->db->trans_complete();        
        return $success;
    }

保存代码工作正常但在编辑时只将新插入的记录保存到数据库中。这是从数据库获取产品详细信息并在编辑时将它们存储在会话中的代码..

function get_update_product(){

        $purchase_id=$_POST['purchase_id'];
        if($purchase_id!=''){
            //$post_array['cart']='';
            $res = $this->db->query("select * from phppos_productdetails WHERE purchase_id='$purchase_id'");
            ?>
            <tr>
                <th>Product  Name</th>
                <th>Quantity</th>
                <th>Unit</th>
                <th>Unit Rate</th>
                <th>Action</th>
            </tr>
            <?php
            $i=0;
            foreach($res->result() as $row ){
                $sess_products[$i]['product_id'] = $row->product_id;
                $sess_products[$i]['quantity'] = $row->quantity;
                $sess_products[$i]['unit'] = $row->unit;
                $sess_products[$i]['unit_rate'] = $row->unit_rate;

                $this->session->set_userdata('sess_products',$sess_products);
                //$post_array['cart']=$this->session->userdata('sess_products');

                $query = $this->db->query("SELECT name FROM phppos_items WHERE item_id='".$row->product_id."'");
                foreach($query->result() as $row1 ){
                    $product_name=$row1->name;

                }
                echo "<tr>";
                echo "<td><input type='hidden'  style='width:80%;'  value='".$row->product_id."' name='product_id[]'/></td>";
                echo "<td><input type='hidden'  style='width:80%;'  value='".$product_name."' name='product_name[]'/></td>";
                echo "</tr>";
                echo "<tr>";
                echo "<td style='width:40%;'>".$product_name."</td>";

                echo "<td><input type='text' class='quantity' style='width:100%;' value='".$row->quantity."' name='quantity[]'/></td>";
                echo "<td><input type='text' class='quantity' style='width:100%;' value='".$row->unit."' name='unit[]'/></td>";
                echo "<td><input type='text' class='quantity' style='width:100%;' value='".$row->unit_rate."' name='unit_rate[]'/></td>";
                echo "<td><a href='javascript:void(0)' rownum='".$i."' class='remove_from_update_cart'><img src='images/close.png'/></a></td>";
                echo "</tr>";
                $i++;

            }


        }
    }

有人对此有任何想法吗?

1 个答案:

答案 0 :(得分:0)

您可以通过以下行

更新codeigniter中的会话
$this->session->set_userdata('your_session_variable',$products);