从数据库中减去PHP变量
我正在使用codeigniter并试图从数据库中减去一个输入值,但运气不好。我目前的模式是:从数据库中减去PHP变量,php,mysql,codeigniter,activerecord,subtraction,Php,Mysql,Codeigniter,Activerecord,Subtraction,我正在使用codeigniter并试图从数据库中减去一个输入值,但运气不好。我目前的模式是: $newstock = $this->input->post('f2'); $itemname = $this->input->post('f1'); $this->db->where('ItemName', $itemname); $this->db->set('Stock
$newstock = $this->input->post('f2');
$itemname = $this->input->post('f1');
$this->db->where('ItemName', $itemname);
$this->db->set('Stock', 'Stock-'.[$newstock], FALSE);
我得到这个错误:
Severity: Notice
Message: Array to string conversion
Filename: models/inventory.php
尝试:
试试这个代码,我不确定这是否会奏效,但我想我给你一个想法
function update(){
$newstock = "Stock-".$this->input->post('f2');
$itemname = $this->input->post('f1');
$data = array("stock" => $newstock);
$this->db->where('ItemName',$itemname);
$this->db->update('inventory', $data);
}
找到了一个解决方案:
$ItemName = $this->input->post('f2');
$query = $this->db->select('Stock')->from('inventory')->get();
foreach ($query->result() as $row)
{
$row->Stock;
}
$oldstock = $row->Stock;
$numtosub = $this->input->post('f2');
$numtosub;
$newstock = $oldstock - $numtosub;
$newstock;
$data = array('Itemname' => $this->input->post('f1'),
'Stock' => $newstock,
);
$data2 = array('Itemname' => $this->input->post('f1'),
'QuantitySold' => $this->input->post('f2'),
'Date' => standard_date()
);
$this->db->insert('transactions', $data2);
$itemname = $this->input->post('f1');
$this->db->where('ItemName', $itemname);
$this->db->update('inventory', $data);
谢谢你们的帮助 按拆下支架$newstock@Denmark当我这样做的时候,我会遇到这样的错误:你的SQL语法有错误;检查与MySQL服务器版本对应的手册,了解在第1行更新
Stock
SET`=“WHEREStock
='Stock-”中使用的“Stock`='Stock-”附近的正确语法。您使用了什么代码?您能给我一个您想要运行的SQL查询吗:updateinventory SET stock=stock-$this->input->post('f2'),其中ItemName=$this->input->post('f1'))
$ItemName = $this->input->post('f2');
$query = $this->db->select('Stock')->from('inventory')->get();
foreach ($query->result() as $row)
{
$row->Stock;
}
$oldstock = $row->Stock;
$numtosub = $this->input->post('f2');
$numtosub;
$newstock = $oldstock - $numtosub;
$newstock;
$data = array('Itemname' => $this->input->post('f1'),
'Stock' => $newstock,
);
$data2 = array('Itemname' => $this->input->post('f1'),
'QuantitySold' => $this->input->post('f2'),
'Date' => standard_date()
);
$this->db->insert('transactions', $data2);
$itemname = $this->input->post('f1');
$this->db->where('ItemName', $itemname);
$this->db->update('inventory', $data);