交易完成时减少库存 codeigniter

decrease the stock when the transaction is completed codeigniter

我想在交易完成后减少库存产品,我尝试使用数据中的 foreach,但库存没有正确减少

控制器:

for ($i=0; $i < $count ; $i++) { 

//SAVE DETAIL PENJUALAN
$data[] = array(
    'nonota'     => $this->input->post('nonota',TRUE),
    'id_brg'     => $this->input->post('kd_brg',TRUE)[$i],
    'nama_brg'   => $this->input->post('nama',TRUE)[$i],
    'jml_brg'    => $this->input->post('jml',TRUE)[$i],
    'harga_brg'  => $this->input->post('harga',TRUE)[$i], 
);

//DELETE CART
$cart[] = array(
    'rowid' => $this->input->post('rowid',TRUE)[$i],
    'qty'   => 0 
);

$update[] = array(
'id'    => $this->input->post('kd_brg',TRUE)[$i],
'stok'  => 'stok' -  $this->input->post('jml',TRUE)[$i]
);


$this->M_penjualan->updatestock($update,'tbl_barang');

}

型号

function updatestock($update) {
        $this->db->update_batch('tbl_barang',$update,'id');
    }

1.Maybe 产品 ID 与您从迭代中获得的产品 ID 不同。

2.Take 看看这一行:

'stok'  => 'stok' -  $this->input->post('jml',TRUE)[$i]

你改变了一个字符串的值,因为'stok'是一个字符串。如果您要 $stok - $this->input... ,那将是另外一回事。当然,如果库存代表某个数字。

3.Also 我会先尝试用简单的更新来更新。 $this->db->set('stok', 'stok-'.$stok, false); - 在模型函数的某处。