codeigniter 我怎样才能减去我在数据库中的数量?

u0sqgete  于 2023-01-03  发布在  其他
关注(0)|答案(1)|浏览(89)
    • bounty将在5天后过期**。回答此问题可获得+50的声誉奖励。wloleo正在寻找来自声誉良好来源的答案

为什么我不能修复我的股票历史记录中的减值?我是Codeigniter 3的新手。
我的table是这样的
| 产品名称|数量|地位|
| - ------| - ------| - ------|
| REG冲绳|五百|主动|
| REG冲绳|五百|主动|
在我的POS机中,我的型号代码如下:为什么每当我输入数量800时,我都希望stock_history表看起来像这样
| 产品名称|数量|地位|
| - ------| - ------| - ------|
| REG冲绳|三百|主动|
但每当我处理输入时,表格就会变成这样
| 产品名称|数量|地位|
| - ------| - ------| - ------|
| REG冲绳|五百|主动|
在我的localhost表中它是这样的
| 产品名称|数量|地位|
| - ------| - ------| - ------|
| REG冲绳|-200人|主动|
| REG冲绳|五百|主动|
我该如何解决这个问题?

$count_product = count($this->input->post('product'));
for($x = 0; $x < $count_product; $x++) {
$prodid = $this->input->post('product')[$x];
$prod_quan = $this->model_products->getProductData($prodid);
  if($prod_quan['qty']>=(int)$this->input->post('qty')[$x])
  {
    $this->db->where('id', $id);
    $update = $this->db->update('orders', $data);

    $this->db->where('order_id', $id);
    $this->db->delete('order_items');
    // Loop through the order items and keep track of the total quantity for each product
    $product_quantities = array();
    $count_product = count($this->input->post('product'));
    for($x = 0; $x < $count_product; $x++) {
      $product_id = $this->input->post('product')[$x];
      $qty = $this->input->post('qty')[$x];

      // If this is the first time we are seeing this product, initialize its quantity to 0
                        if (!isset($product_quantities[$product_id])) {
                            $product_quantities[$product_id] = 0;
                        }

                        // Add the quantity of this item to the total quantity for this product
                        $product_quantities[$product_id] += $qty;
                    }

                    // Loop through the product quantities and update the `order_items` table
                    foreach ($product_quantities as $product_id => $qty) {
                        $data = array(
                            'qty' => $qty
                        );
                        $this->db->where('product_id', $product_id);
                        $this->db->where('order_id', $id);
                        $this->db->update('order_items', $data);

                        $this->db->where('product_id', $product_id);
                        $this->db->where('st_code', $id);
                        $this->db->update('stock_history', $data);
                    }

                    $count_product = count($this->input->post('product'));
                    for($x = 0; $x < $count_product; $x++) {
                        $items = array(
                            'order_id' => $id,
                            'product_id' => $this->input->post('product')[$x],
                            'qty' => $this->input->post('qty')[$x],
                            'rate' => $this->input->post('rate_value')[$x],
                            'amount' => $this->input->post('amount_value')[$x],
                        );
                        $this->db->insert('order_items', $items);
                        $prodid = $this->input->post('product')[$x];

                        if($prodid){
                            $inputQuantity = (int)$this->input->post('qty')[$x];
                            $this->db->where('id', $prodid);
                            $this->db->set('qty', "qty-$inputQuantity", false);
                            $this->db->update('products');

                            $inputQuantity = (int)$this->input->post('qty')[$x];
                            $this->db->where('product_id', $prodid);
                            $this->db->set('qty', "qty-$inputQuantity", false);
                            $this->db->limit(1);
                            $this->db->update('product_inventory');

                            $inputQuantity = (int)$this->input->post('qty')[$x];
                            $this->db->where('product_id', $prodid);
                            $this->db->set('qty', "qty-$inputQuantity", false);
                            $this->db->limit(1);
                            $this->db->update('stock_history');
                        }
                    }
                    return true;
                }
                else{
                    echo '<script>alert("NOT ENOUGH STOCKS");</script>';
                }
            }

        }
tct7dpnv

tct7dpnv1#

更新product表的步骤

$this->db->set('qty', 'qty-'.$inputQuantity, false);
$this->db->where('id', $prodid);
$this->db->update('products');

更新stock_history的步骤

$this->db->set('qty', 'qty-'.$inputQuantity, false);
$this->db->where('product_id', $product_id);
$this->db->where('st_code', $id);
$this->db->update('stock_history');

相关问题