在 Codeigniter 中更新数组查询
Update array query in Codeigniter
我正在使用 Codeigniter 制作药房系统。我想在购买后更新 table 中几件商品的库存,下面是我到目前为止尝试过的代码。
下面是我的控制器
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;
}
}
但是当我执行代码的时候,出现了这样的信息
我知道我应该将参数转为数组。但我不知道怎么办?感谢您的帮助
希望对您有所帮助:
您的 update
查询应该在 foreach
循环中,因为 quantity
和 medicine
都是一个数组。应该是这样的:
foreach($medicine as $key=>$val)
{
$data[] = array(
'customer' => $customer,
'date' => $date,
'grandtotal' => $grandtotal,
'ref' => $ref,
'medicine' => $val,
'quantity' => $quantity[$key],
'subtotal' => $subtotal[$key],
);
$this->db->set('stock', 'stock-'.$quantity[$key], FALSE);
$this->db->where('medicine', $val);
/* if not works use this
$this->db->where('medicine', $medicine[$key]);
*/
$updated = $this->db->update('table_med');
}
更多:https://www.codeigniter.com/user_guide/database/query_builder.html#updating-data
我正在使用 Codeigniter 制作药房系统。我想在购买后更新 table 中几件商品的库存,下面是我到目前为止尝试过的代码。
下面是我的控制器
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;
}
}
但是当我执行代码的时候,出现了这样的信息
我知道我应该将参数转为数组。但我不知道怎么办?感谢您的帮助
希望对您有所帮助:
您的 update
查询应该在 foreach
循环中,因为 quantity
和 medicine
都是一个数组。应该是这样的:
foreach($medicine as $key=>$val)
{
$data[] = array(
'customer' => $customer,
'date' => $date,
'grandtotal' => $grandtotal,
'ref' => $ref,
'medicine' => $val,
'quantity' => $quantity[$key],
'subtotal' => $subtotal[$key],
);
$this->db->set('stock', 'stock-'.$quantity[$key], FALSE);
$this->db->where('medicine', $val);
/* if not works use this
$this->db->where('medicine', $medicine[$key]);
*/
$updated = $this->db->update('table_med');
}
更多:https://www.codeigniter.com/user_guide/database/query_builder.html#updating-data