2012-10-04 6 views
1

데이터베이스에 삽입 할 때 오류가 있습니다.PHP SQL 삽입 오류

코드 :

dbquery("INSERT INTO site_news_comments (articleid,title,short,comment,timestamp,userid,main,type,topstory) VALUES ($article,'".clean($commentss['title'])."','','".mysql_real_escape_string($_POST['comment'])."',current_timestamp,'".USER_ID."','0','".$commentss['type']."','')"); 

가 dbquery 무시는 상 mysql_query로 정확하게 작동합니다. 내가 수신하고

오류 :이 오류가 발생하는 이유

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 ''title','short',' 

없음 아이디어!

+0

왜 모든 데이터를 먼저 변수에 넣지 않습니까? 나중에 INSERT하는 것이 훨씬 더 쉽습니다. 예 : $ title = clean ($ commentss [ 'title']) – s3polz

+0

쿼리를 인쇄하고 실행하기 전에 빌드를 확인하십시오. – Deepak

답변

2

어떻게 낚시를하는 사람을 교육.

쿼리가 실패하면

은, 당신이해야 할 첫 번째 일은 당신이 보내려고하고있는 쿼리 반향하는 것입니다

$sql = "INSERT INTO site_news_comments (articleid,title,short,comment,timestamp,userid,main,type,topstory) VALUES ($article,'".clean($commentss['title'])."','','".mysql_real_escape_string($_POST['comment'])."',current_timestamp,'".USER_ID."','0','".$commentss['type']."','')"; 

echo $sql; 

그것은 일반적으로 최종 쿼리 뭐가 잘못 꽤 명백를; 귀하의 질의 및 일반적으로 MySQL이 불만을 제기하는 영역에 대한 동적 인 내용에 특히주의하십시오.

그래도 괜찮 으면 reserved words과 같이 이스케이프가 필요한 단어를 찾습니다.

결론

이 코드 mysql을 바라 보았다 데, 그 문제는 $article로 거짓말을하고 쿼리에 문제가 발생한다는 결론해야합니다. 당신은 아마 경우에,뿐만 아니라 그것을 탈출한다 :

추천

당신은/mysqli과 준비된 문을 사용하여 PDO에 대해 배워야한다

: 도움말들에 대한

// PDO example 
$stmt = $db->prepare('INSERT INTO site_news_comments (articleid, title, short, comment, timestamp, userid, main, type, topstory) VALUES (:article, :title, :short, :comment, CURRENT_TIMESTAMP, :user, :main, :type, :topstory)'); 
$stmt->execute(array(
    ':article' => $article, 
    ':title' => $commentss['title'], 
    ':short' => '', 
    ':comment' => $_POST['comment'], 
    ':user' => USER_ID, 
    ':main' => 0, 
    ':type' => $commentss['type'], 
    ':topstory' => '', 
)); 
+0

아티클은 URL에 정의되어 있으므로 비어 있지 않으며 기사와 함께 할 다른 것들이 표시됩니다. – zuc0001

+0

@ zuc0001 응답의 나머지 부분도 읽고, 쿼리를 반향하고 살펴 봅니다. –

+0

Ok, 쿼리를 수행했습니다. 그것은 $ _POST [ 'comment']에 대한 하나의 오류를 제외하고는 괜찮 았지만 (왜냐하면 나는 양식을 제출하지 않았기 때문에 thats), articleid가 '1'이 아닌 $ article에 오류가있었습니다. '1 /'을 가졌습니다. 어떤 아이디어? – zuc0001

2

편집
너무 빨리 처음 읽습니다. 오류가 열 목록에 나타나지 않으면 값 목록에있는 것처럼 보입니다. 쿼리에서 구문 오류가 발생할 수있는 유일한 위치는 $article이 비어 있거나 비 숫자 등의 비 위생적 데이터입니다. 쿼리에 따옴표를 추가 및/또는 확인하십시오 그것은 적어도 기본 값이 있습니다

$article = (empty($article) || !is_numeric($article)) ? 0 : $article; 
dbquery("... VALUES ('".$article."', '".clean($commentss['title'])."', '', '".mysql_real_escape_string($_POST['comment'])."', current_timestamp, '".USER_ID."', '0', '".$commentss['type']."', '')"); 

원래 대답을 MySQL이 사용 reserved words의 목록이 있습니다

당신이 그들을 사용하는 경우, 그 열 이름의 경우 backticks로 이스케이프해야합니다. 해결하려면 그들 모두를 업데이트

시도 :

dbquery("INSERT INTO site_news_comments (`articleid`, `title`, `short`, `comment`, `timestamp`, `userid`, `main`, `type`, `topstory`) VALUES ... 
+0

감사합니다. 비록 나는 여전히 정확히 같은 오류를 받고있다. 내 웹 사이트의 "뉴스"시스템에서 사용됩니다. 기사의 "frontpage"에서 코멘트를 올리면 ok가 삽입됩니다. 그러나 페이지 2와 같은 다른 주석 페이지에서 삽입하면 오류가 발생합니다. 나는 왜 그런지 모르겠다. – zuc0001

+0

-1. 열 이름을 피하는 것이 좋은 조언이지만 근본 원인은 아닙니다. –

+0

@Jack 그래, 나는 예약어 목록과 비교하여 모든 열 이름을 처리했지만 실제로는 일치하지 않는다 ('timestamp' 제외). 그러나 그것은 "괜찮"입니다. 내 대답을 업데이 트했지만 실제로는 당신이 펀치 = P에 저를 이길 것 같아요 – newfurniturey

0

감사합니다! 하지만 문제가 해결되었습니다!

문제의 원인은 "URL"이었습니다.URL은이었다

뉴스/1/& 나는 $ 문서를 삽입 할 때 2

그래서, 그것은 '1 /'로 온 = 페이지는 ID가 1/1이 아닌 때문이라고 생각했기 때문에이 있었다 URL의

그래서 난 그냥

뉴스/1 & 페이지 = 2 개

감사로 변경했습니다!

0
//change this line : 
dbquery("INSERT INTO site_news_comments (articleid,title,short,comment,timestamp,userid,main,type,topstory) VALUES ($article,'".clean($commentss['title'])."','','".mysql_real_escape_string($_POST['comment'])."',current_timestamp,'".USER_ID."','0','".$commentss['type']."','')"); 

//to this : (surround $articleid with single quote) 
dbquery("INSERT INTO site_news_comments (articleid,title,short,comment,timestamp,userid,main,type,topstory) VALUES ('".$article."','".clean($commentss['title'])."','','".mysql_real_escape_string($_POST['comment'])."',current_timestamp,'".USER_ID."','0','".$commentss['type']."','')");