我正在使用 Codeigniter 制作药房系统。我想在购买后更新表中几个项目的库存,下面是我到目前为止尝试过的代码。
下面是我的控制器
function add_invoice(){
$customer = $this->input->post('customer');
$date = date("Y-m-d",strtotime($this->input->post('date')));
$grandtotal = $this->input->post('grandtotal');
$ref = rand(1111111111,9999999999);
$medicine = $this->input->post('medicine');
$quantity = $this->input->post('quantity');
$subtotal = $this->input->post('subtotal');
foreach($medicine as $key=>$val){
$data[] = array(
'customer' => $customer,
'date' => $date,
'grandtotal' => $grandtotal,
'ref' => $ref,
'medicine' => $val,
'quantity' => $quantity[$key],
'subtotal' => $subtotal[$key],
);
}
$this->my_model->decrement_item($medicine, $quantity);
$this->db->insert_batch('table_invoice', $data);
}
这是我下面的模型:
function decrement_item($medicine, $quantity)
{
$q = "UPDATE table_med SET stock = stock - ? WHERE medicine = ?";
$this->db->query($q, [$quantity, $medicine]);
if($this->db->affected_rows() > 0){
return TRUE;
}
else{
return FALSE;
}
}
我知道我应该将参数转换为数组。但我不知道怎么做?谢谢你的帮助