1

我已经使用代码几个小时了,只是看不到哪里出错了。这是有问题的代码

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

这是报告的错误

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

我想知道我是否缺少一些简单的东西?我怀疑这是因为 $iscorrect1 是布尔类型,我尝试以多种方式对其进行编辑,但仍然出现同样的错误。

/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)
4

1 回答 1

6

)您在查询结束时忘记了 a :

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

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

于 2013-01-27T12:48:25.160 回答