我正在尝试用CakePHP执行SQL更新。这是我的代码:

$sql = "
  UPDATE carts
  SET
    qty = ".$this->data['Cart']['qty'].",
    process = 'UnPaid'
  WHERE ct_session_id = '".$this->data['Cart']['ct_session_id']."'
    AND product_id = '".$this->passedArgs['pd_id']."'
    AND key = '".$this->Session->read('Cart.key', $newCartkey)."'
";
$this->Cart->query($sql);

我得到这个错误:
SQL Error: 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key = 'bwfgkxms'' at line 3

我的代码生成的查询是:
UPDATE carts
SET
  qty = 111,
  process = 'UnPaid'
WHERE ct_session_id = '3254430f577669bb8ecdb8b8aadf1b96'
  AND product_id = '51'
  AND key = 'bwfgkxms'

最佳答案

key是MySQL中的一个reserved word,您需要在列名中用反勾号将其括起来。

$sql = "
 UPDATE carts
 SET qty = ".$this->data['Cart']['qty'].", process = 'UnPaid'
 WHERE ct_session_id = '".$this->data['Cart']['ct_session_id']."'
   AND product_id = '".$this->passedArgs['pd_id']."'
   AND `key` = '".$this->Session->read('Cart.key', $newCartkey)."'
";

关于php - cakephp SQL帮助!哪里?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11953044/

10-15 12:32