Postgresql INSERT查询的PHP语法错误

时间:2013-01-27 12:36:47

标签: php sql postgresql

我一直在使用代码几个小时,只是看不到我有错误的地方。这是违规代码

$answercreatequery = pg_query("INSERT INTO answer (answerid, questionid, adescription, afilelocation, iscorrect) VALUES( default, '".$thisquestionid."', '".$adescription1."', '".$afilelocation."', '".$iscorrect1."' ");

这是报告的错误

  

警告:pg_query()[function.pg-query]:查询失败:错误:语法   输入结束时的错误LINE 1:... on,iscorrect)VALUES(默认值为'37',   'kyfhdkj','没',''^ in   /ceri/homes1/m/mtp4/public_html/mmp/Quizmaker/Clientside/questioncreatescript.php   在第167行

我想知道是否有一些我想念的简单的东西?我怀疑是因为$ iscorrect1是boolean类型,我尝试过多次编辑它,但我仍然遇到同样的错误。

/ d答案表

Column     |          Type          |                         Modifiers                         
---------------+------------------------+-----------------------------------------------------------

 answerid      | integer                | not null default nextval('answer_answerid_seq'::regclass)
 questionid    | integer                | not null
 adescription  | character varying(200) | not null
 afilelocation | character varying(200) | not null
 iscorrect     | boolean                | not null
Indexes:
    "answer_pkey" PRIMARY KEY, btree (answerid)
Foreign-key constraints:
    "answer_questionid_fkey" FOREIGN KEY (questionid) REFERENCES question(questionid)

1 个答案:

答案 0 :(得分:5)

您在查询结束时忘记了)

$answercreatequery = pg_query("INSERT INTO answer (answerid, questionid, adescription, afilelocation, iscorrect) VALUES( default, '".$thisquestionid."', '".$adescription1."', '".$afilelocation."', '".$iscorrect1."' ");
........................................................................................................................................................................................................insert ) here ^

除此之外,您可以简单地省略要使用默认值的列:

$answercreatequery = pg_query("INSERT INTO answer
  (questionid, adescription, afilelocation, iscorrect) VALUES
  ('".$thisquestionid."', '".$adescription1."', '".$afilelocation."', '".$iscorrect1."')");

此外,你应该真的使用pg_query_params(而不是转义或有一个SQL注入漏洞):

$answercreatequery = pg_query_params('INSERT INTO answer
  (questionid, adescription, afilelocation, iscorrect) VALUES ($1, $2, $3, $4)',
  array($thisquestionid, $adescription1, $afilelocation, $iscorrect1));

这样做也使您的代码更具可读性。