简体   繁体   中英

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

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.

/d answer table

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):

$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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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