1

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)
3
  • I would first try hard-coding each of $adescription1, $afilelocation, etc. until you discover which of the four is the offender. It looks like it's probably $iscorrect1, so maybe try hard-coding that and see if the problem goes away. Commented Jan 27, 2013 at 12:41
  • Also, pasting a \d answer in your question might be helpful. Commented Jan 27, 2013 at 12:42
  • It was defined as serial. I've been using it for my other INSERT queries and it hasn't caused a problem. Commented Jan 27, 2013 at 12:42

1 Answer 1

6

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.

1
  • Thank you very much! I feel bad for making such a simple error! Also thank you for making me aware of pg_query_params, I'm going to spend some time looking into it further in the PHP manual. Commented Jan 27, 2013 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.