从postgresql数据库填充php下拉框

I need to populate a dropdown list of book titles from my postgreSQL database using a query such as SELECT title FROM books WHERE ownedBy = [users facebook ID] and then use the selection by the user to display the rest of the information on that book. The page is a facebook app, which is how I'm getting the facebook ID.

This is the relavent section of code so far, mostly created from various answers to similar questions I have found.

<form action="updateform.php" method="post">
<select name="booktitle" id="booktitle">
<option>Select book</option>
<?php
$db = pg_connect("host=ec2-54-243-190-226.compute-1.amazonaws.com port=5432 dbname=d6fh4g6l0l6gvb    user=zmqygfamcyvhsb password=1Apld4ivMXSK8JZ_8yL7FwIuuz sslmode=require options='--client_encoding=UTF8'") or   die('Could not connect: ' . pg_last_error());
$sql = pg_query("SELECT title FROM books WHERE ownedby='$user_id'";
while ($row = pg_fetch_assoc($sql)) {
echo '<option value="'.htmlspecialchars($row['title']).'"></option>';}
pg_close($db);
?>
</select>
<input type="hidden" name="userid" id="userid" value="<?php echo htmlspecialchars($user_id); ?>">
//other form elements here
</form>

UPDATED: I hadn't closed the hidden field, so that seemed to fix the error where it wasn't showing up. The dropdown list is not populating, but I am not getting any errors in the server log.

If anyone can help me get the drop down box part to work, that would be great for now, I'll work on figuring the rest out myself once this part is working.

You should write

$sql = pg_query($db, "SELECT title FROM books WHERE ownedby='$user_id'");

Also, the displayed name in your options has to be specified.

echo '<option value="'.htmlspecialchars($row['title']).'">'.htmlspecialchars($row['title']).'</option>';

in your query '$user_id' is null. first check the value of '$user_id' carefully. then it will work.