For a Quiz website, I am creating a record for a question, and want to get it’s questionid straight after so I can use it in creating answer records. The questionid created at INSERT is a serial value automatically generated.
I am new to PHP and cannot figure out how I may get this value, as this id is the only unique way to id this newly created record. Is there anyway to do so?
This is my code for creating the new record.
$questioncreatequery = pg_query("INSERT INTO question (questionid, quizid, questiontype, qdescription, qfilelocation, noofanswers, answertype) VALUES( default, '".$thisquizid."', '".$questiontype."', '".$qdescription."', '".$qfilelocation."', '".$noofanswers."', '".$answertype."')");
Thanks for your time.
The easiest way to get what you need is to use the
RETURNINGclause, to return a value from yourINSERTstatement like that:Then you can use
$questioncreatequeryjust like any query result and get your value: