簡體   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