cakephp SQL帮助!哪里?

DAN*_*LEE 1 php mysql cakephp

我正在尝试在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); 
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

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
Run Code Online (Sandbox Code Playgroud)

我的代码生成的查询是:

UPDATE carts
SET
  qty = 111,
  process = 'UnPaid'
WHERE ct_session_id = '3254430f577669bb8ecdb8b8aadf1b96'
  AND product_id = '51'
  AND key = 'bwfgkxms'
Run Code Online (Sandbox Code Playgroud)

nic*_*ckb 8

key在MySQL中是一个保留字,你需要在列名中用反引号括起来.

$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)."'
";
Run Code Online (Sandbox Code Playgroud)

  • 这是一项协作努力:-D - 可能也值得一提Bobby Tables,看起来像我未经过用户输入的怀疑...... (4认同)