繁体   English   中英

Postgresql INSERT查询的PHP语法错误

[英]PHP Syntax Error with Postgresql INSERT query

我已经使用了几个小时的代码,只是看不到哪里有错误。 这是令人讨厌的代码

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

这是报告的错误

警告:pg_query()[function.pg-query]:查询失败:错误:输入行1末尾的语法错误:...打开,不正确)VALUES(默认值,'37','kyfhdkj','none', 167行/ceri/homes1/m/mtp4/public_html/mmp/Quizmaker/Clientside/questioncreatescript.php中的^

我想知道是否缺少一些简单的东西? 我怀疑这是因为$ 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)

您在查询末尾忘记了)

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM