简体   繁体   English

Postgresql INSERT查询的PHP语法错误

[英]PHP Syntax Error with Postgresql INSERT query

I have been working with code for hours and just cannot see where I have an error. 我已经使用了几个小时的代码,只是看不到哪里有错误。 This is the offending code 这是令人讨厌的代码

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

And this is the error reported 这是报告的错误

Warning: pg_query() [function.pg-query]: Query failed: ERROR: syntax error at end of input LINE 1: ...on, iscorrect) VALUES( default, '37', 'kyfhdkj', 'none', '' ^ in /ceri/homes1/m/mtp4/public_html/mmp/Quizmaker/Clientside/questioncreatescript.php on line 167 警告:pg_query()[function.pg-query]:查询失败:错误:输入行1末尾的语法错误:...打开,不正确)VALUES(默认值,'37','kyfhdkj','none', 167行/ceri/homes1/m/mtp4/public_html/mmp/Quizmaker/Clientside/questioncreatescript.php中的^

I am wondering if there is something simple I am missing? 我想知道是否缺少一些简单的东西? I have suspected it was because $iscorrect1 is type boolean and I have tried editing it in many ways but still I get the same kind of error. 我怀疑这是因为$ iscorrect1是boolean类型,并且我尝试了多种编辑方式,但是仍然遇到相同的错误。

/d answer table / 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)

You forgot a ) at the end of your query: 您在查询末尾忘记了)

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

Besides that, you can simply omit the column where you want the default value to be used: 除此之外,您可以简单地省略要使用默认值的列:

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

Additionally, you should really use pg_query_params (instead of escaping or having a sql injection hole): 另外,您应该真正使用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));

Doing so also makes your code much more readable. 这样做还使您的代码更具可读性。

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

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