[英]Store array and update record in database using codeigniter
我正在存儲動態數組以及更新數據庫中的“ max_quantity”表。 問題是,當更新記錄時,它將對所有數量值求和,並從“最大數量表”的第一個值中減去它。 但是我想將數量數組減去數據庫中存在的數量值。
這是我的代碼
<?php $i=1; foreach($result as $row){
?>
<td class="pr-right" style='width:130px; text-align: center; '>
<input type="text" min="1" step="1" name="quantity[]" step="1" class="container" value="" onfocus="this.value = '';" onblur=";" style="width: 60px" id="quantityT<?php echo $i;?>" onkeyup="CalculatePrice (<?php echo $i;?>,<?php echo $row->max_quantity; ?>)">
<br>(Quantity Available = <?php echo $row->max_quantity; ?>)
</td>
<?php
$i++;
} ?>
這是我在控制器中的代碼
public function get_insert_order(){
$quantity=$this->input->post('quantity');
for($i=0; $i<count($ingredient); $i++){
$data = array(
"user_quantity" =>$quantity[$i],
);
$response = $this->bulk_recipe->insert_bulk_order($data);
$max = $this->db->query("select bulk_delivery.max_quantity from bulk_delivery")->result_array();
foreach ($max as $rows) {
$calc = $rows['max_quantity'];
$calc = $calc - $quantity[$i];
}
$this->db->query("UPDATE bulk_delivery SET max_quantity =$calc");
}
這是我在模型中的代碼
function insert_bulk_order($form_data)
{
$this->db->insert('bulk_delivery_order',$form_data);
if ($this->db->affected_rows() == '1')
{
return TRUE;
}
return FALSE;
}
這是更新之前的前端視圖。
這是更新后的前端
請指導我如何將每個數量分別減去其max_quantity。
這就是您要尋找的...!
$this->db->set();
此功能使您可以設置插入或更新的值。
可以使用它代替將數據數組直接傳遞給插入或更新函數:
$this->db->set('field', 'field+1', FALSE);
$this->db->insert('mytable');
// gives INSERT INTO mytable (field) VALUES (field+1)
$this->db->set('field', 'field+1');
$this->db->insert('mytable');
// gives INSERT INTO mytable (field) VALUES ('field+1')
https://ellislab.com/codeigniter/user-guide/database/active_record.html
您的代碼有幾個錯誤,您正在從db中選擇所有行,並每次都更新它們。 您的代碼應該是這樣的...
$max = $this->db->query("select bulk_delivery.max_quantity from bulk_delivery where some_id = $i")->row_array();
$calc = $rows['max_quantity'];
$calc = $calc - $quantity[$i];
$this->db->query("UPDATE bulk_delivery SET max_quantity =$calc WHERE some_id = $i");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.