2012-08-14 2 views
1

CakePHP에서 SQL 업데이트를 수행하려고합니다.cakephp SQL HELP! 어디에?

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 

내 코드가 생성하는 쿼리는 다음과 같습니다 :이 오류 얻을

$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); 

: 여기 내 코드입니다

UPDATE carts 
SET 
    qty = 111, 
    process = 'UnPaid' 
WHERE ct_session_id = '3254430f577669bb8ecdb8b8aadf1b96' 
    AND product_id = '51' 
    AND key = 'bwfgkxms' 
+1

: 데이터를 업데이트 (및/또는 일반적으로 데이터를 저장)에

$this->Cart->updateAll( array( 'Cart.qty' => $this->data['Cart']['qty'], 'Cart.process' => 'UnPaid'), array( 'Cart.ct_session_id' => $this->data['Cart']['ct_session_id'], 'Cart.product_id' => $this->passedArgs['pd_id'], 'Cart.key' => $this->Session->read('Cart.key', $newCartkey) ) ); 

자세한 내용 :

CakePHP의 방법은 (모든 혜택과 함께 다음과 같은 규칙에 포함) CakePHP,하지만 모든 관습을 무시하는 것은 아마 좋은 생각이 아닙니다. (CakePHP의 save() 기능을 사용하십시오 -이 같은 두통에서 당신을 구할 것입니다) – Dave

답변

8

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)."' 
"; 
+1

@DaveRandom 당신은 저를 때려 눕 힙니다! : P 고마워. – nickb

+4

그것은 공동 작업입니다 .-D - Bobby Tables를 언급 할 가치가 있습니다. 이스케이프 처리되지 않은 사용자 입력이 저에게 ... – DaveRandom