HTML forms and Postgres

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I am new to PHP but not so new to Postgres. If someone can either
direct me to some howto or even provide me with an example, I would be

I would like to know if it is possible to create an HTML form text box
that can draw information out of the database and display it in the
HTML form text box. I have purchased a few books on the integration of
Postgres and PHP but can find no such example. Maybe there is a better
way of doing this?

My theory is that if I can draw this information from the database and
have it displayed in the HTML form text box, this same information can
be UPDATED if need be from the same page. Any information or a point in
the right direction, again would be appriciated.


Re: HTML forms and Postgres

Quoted text here. Click to load it

Most of the books I've seen on PHP and databases use the MySQL database  
rather than Postgres.  But, the approach is the same, especially if you  
use a database abastraction layer like the PEAR DB class.  I chose to  
use a MySQL layer from a php library site.  I'm really surprised that  
the book you have doesn't supply examples.  I'd return it to where you  
bought it and complain to the seller that it's useless.  If you paid  
with a credit card, contest the charge.

If you go to the O'Reilly site ( ), there are _lots_ of  
books on php.  Or find a local technical book store and browse on your  
own to find what suits your needs.

And to clarify your assumption about PHP and databases: you extract the  
information from the database with a SQL statement, then write the  
information out in properly formatted HTML.  This requires you to  
understand how your database works, how to construct a HTML page, and  
how to program in PHP.  Lots of books to buy...

DeeDee, don't press that button!  DeeDee!  NO!  Dee...

Re: HTML forms and Postgres

I have purchased 6 books. 3 being from and still nothing on
textboxes and database. Can anyone else help?
Michael Vilain wrote:
Quoted text here. Click to load it

Re: HTML forms and Postgres

fjm67 wrote:
Quoted text here. Click to load it

Look for these functions on for information:


pg_pconnect will connect to the database. Unlike MySQL, PostgreSQL
requires a seperate connection for each database even on the same
server so if you will be using multiple databases you will need
multiple pg_pconnect calls. If pg_pconnect returns false it failed.
Otherwise it will give you the connection object that you need to pass
to the rest of the functions to interact with the database you
connected to.

$dcn = pg_pconnect('host=localhost port=5432 dbname=mydb user=foo
if ($dcn === false) die('connection error');

Next, make sure after you do pg_query you make a check for errors.
pg_query returns false when it fails, otherwise it returns a result
resource. I recommend always specifying the schema name for each table
and putting double-quotes around schema, table and column names. The
SQL for pg_query is going to be something like:

$sql = 'SELECT "MyTextField" FROM "MySchema"."MyTable";';
$res = pg_query($dcn, $sql);
if ($res === false) die('query error');

htmlspecialchars was used to ensure that any quotes in the string
returned from the database don't cause problems with our HTML output.
If you inject database data into a href attribute of an a tag (or into
any other url) you should use urlencode instead. Use pg_fetch_assoc
together with a while loop:

while ($row = pg_fetch_assoc($res)) {
echo '<input type="text"
value="'.htmlspecialchars($row['MyTextField']).'" />';

I usually process form data in the same file that generates the form
itself. I use a submit button with the name "Action" to handle
different commands. Processing form data works like this:

if (isset($_POST['action'])) {
 // if action has been received we are receiving a submission
 switch ($action) {
  case 'Update':
    $sql = 'UPDATE "MySchema"."MyTable" SET "MyTextField" =
''.pg_escape_string($_POST['MyTextField']).'' WHERE
    $res = pg_query($dcn, $sql);
    if ($res === false) die('update error');

None of the above has been tested and is from memory. But hopefully
that's enough to get you started.


Quoted text here. Click to load it

Re: HTML forms and Postgres

Quoted text here. Click to load it

This is slightly misleading. With PostgreSQL you connect to a "database",  
but within each "database" there can be multiple "schemas". With MySQL you  
connect to "server", and with each server you can have multiple "databases".  
What is called a "schema" in PostgrSQL is called a "database" in MySQL. This  
is a typical case of different organisations using the same word to mean  
different things, thus causing confusion. I believe in the SQL standard that  
"database" and "schema" mean the same thing, while a collection of "database  
schemas" is known as a "catalog".

Tony Marston

Quoted text here. Click to load it

Site Timeline