2016-09-18 6 views
1

데이터베이스에서 셀을 리드하는 작은 스크립트가 있습니다. 그러나 오류 (1064) 그것은 다음과 같습니다 원인 : 내가 PHP Json to MySql (opencart)

2016-09-18 20:43:06 - PHP Notice: Error: 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 'option = '{"options":{"product_id":"176","colors":["000000"],"print":{"colors":{' at line 1<br />Error No: 1064<br />UPDATE oc_cart SET option = '{"options":{"product_id":"176","colors":["000000"],"print":{"colors":{"front":["FF0000"]},"elements":{"front":[{"width":"52.9375","height":"25.0938","type":"text"}]}},"attributes":{"sizememos":"0"},"attribute":[["0","0"]],"print_type":"screen","quantity":"0","design":{"vectors":{"front":[{"type":"text","width":"54.9375px","height":"27.0938px","top":"151px","left":"86px","zIndex":"1","svg":"<svg width="54.9375" height="27.09375" viewBox="0 0 54.9375 27.09375" xmlns="http:\/\/www.w3.org\/2000\/svg" xmlns:xlink="http:\/\/www.w3.org\/1999\/xlink"><g id="0.15760551612925844"><text fill="#FF0000" stroke="none" stroke-width="0" stroke-linecap="round" stroke-linejoin="round" x="" y="" text-anchor="start" font-size="24px" font-family="arial" data-textcurve="1" data-itemzoom="1 1" data-textspacing="0"><textPath xmlns:xlink="http:\/\/www.w3.org\/1999\/xlink" xlink:href="http:\/\/138.68.62.219\/Buy-Hanes-T-shirt-PC61LS#textPath-item-0"><tspan dy="0">Hello<\/tspan><\/textPath><\/text><\/g><defs><path id="textPath-item-0" d="M 0.125 22.117808976867764 A 3093.9720937064453 3093.9720937064453 0 0 1 54.124314613414626 22.117808976867764"><\/path><\/defs><\/svg>","rotate":"0","text":"Hello","color":"#FF0000","fontFamily":"arial","align":"center","outlineC":"none","outlineW":"0"}]},"images":{"front":"cart-designes\/2016\/09\/\/cart-front-1474230421.png","back":"cart-designes\/2016\/09\/\/cart-back-1474230421.png"}},"fonts":""}}' WHERE cart_id = '387' AND customer_id = '0' AND session_id = 'kkfj9svfssdnsph8pf8i5atjn3' in /var/www/html/system/library/db/mysqli.php on line 41 

어떤 이유 로그

에서 볼 무엇 그게

public function update($cart_id, $quantity) { 
    $this->db->query("UPDATE " . DB_PREFIX . "cart SET quantity = '" . (int)$quantity . "' WHERE cart_id = '" . (int)$cart_id . "' AND customer_id = '" . (int)$this->customer->getId() . "' AND session_id = '" . $this->db->escape($this->session->getId()) . "'"); 
    //starts here 
    $cart_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "cart WHERE cart_id = '" . (int)$cart_id. "' AND session_id = '" . $this->db->escape($this->session->getId()) . "'"); 
    $option= ($cart_query->row['option']);//success 
    $this->db->query("UPDATE " . DB_PREFIX . "cart SET option = '" . (string)$option . "' WHERE cart_id = '" . (int)$cart_id . "' AND customer_id = '" . (int)$this->customer->getId() . "' AND session_id = '" . $this->db->escape($this->session->getId()) . "'");//error 
} 

를? 나는이 셀에 이미있는 데이터를 저장하고있다. 옵션 열은 UTF-8, 긴 텍스트

+0

입니다 다시 큰 따옴표가 "* 큰 따옴표로 묶여 있습니다."} ' 별들로 둘러싸인 부분은'innerString'의 일부가 아니며, 컴퓨터가 그걸로 무엇을해야할지 모르기 때문에 약간의 오류가 발생할 것입니다. 내부 큰 따옴표 앞에 백 슬래시를 넣을 수 있으면 문제를 해결해야합니다. – Howzieky

+0

@Howzieky'addslashes''json_decode json_encode''htmlspecialchars' 등을 시도했습니다 .... 성공적으로 디코드 및 인코딩 된 JSON을 읽었을 것입니다. 아마도 저장하는 더 좋은 방법이나 쿼리 문자열 제한이있을 것입니다 ... – tttaaabbb

답변

0

오류 1064는 MySQL 쿼리의 구문 오류를 가리 킵니다. 사용자의 JSON 문자열에 이스케이프 처리가되지 않은 견적이있는 것 같습니다. 옵션의 값을 수정하려고 시도한 것입니다. 일부 짧은 값과 함수를 다시 호출하십시오.

가장 좋은 방법은 매개 변수화 된 쿼리를 사용하는 것입니다,이 모든 이스케이프 작업에서 당신을 절약 할 가장 중요한 이것은 당신의 쿼리처럼 보이는 방법의 예에 불과 SQL 주입

에서 몇 가지 보호 기능을 제공 :

$sql="UPDATE table SET opton=:option WHERE cart_id=:id AND customer_id=:customer_id and session_id=:session_id"; 
$parameters = array(
       'option'=>$option, 
       'cart_id'=>$cart_id, 
       'customer_id'=>$customer_id, 
       'session_id'=>$session_id 
      ); 

$sql = $this->db->prepare($sql); 
$sql->execute($parameters); 

당신은 PHP 공식 문서

http://php.net/manual/en/mysqli.quickstart.prepared-statements.php http://php.net/manual/en/pdo.prepared-statements.php

,536에 대한 자세한 내용을 찾을 수 있습니다

해피 코딩 친구 :)

0

OPTION 코즈 때문에 MySql 구문의 일부로 작동하지 않습니다. {innerString : - * 내가 두 번을 사용하고 있습니다 "> 내가 문자열입니다,하지만 문제는 바로 여기에있다"문제가 당신이 outerThing ''와 같은 문자열을 가지고 것 같다 작업

$customer_id=(int)$this->customer->getId(); 
    $session_id=$this->db->escape($this->session->getId()); 
    $this->db->query("UPDATE " . DB_PREFIX . "cart SET `option` = '" . (int)$quantity . "' WHERE cart_id = '" . (int)$cart_id . "' AND customer_id = '" . $customer_id . "' AND session_id = '" . $session_id . "'");