By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,640 Members | 2,194 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,640 IT Pros & Developers. It's quick & easy.

HTML forms and Postgres

P: n/a
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
grateful.

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.

Frank

Jul 15 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
I have purchased 6 books. 3 being from ora.com and still nothing on
textboxes and database. Can anyone else help?
Michael Vilain wrote:
In article <11**********************@75g2000cwc.googlegroups. com>,
"fjm67" <fj*********@yahoo.comwrote:
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
grateful.

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.

Frank

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 (http://www.ora.com), 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...
Jul 16 '06 #2

P: n/a

fjm67 wrote:
I have purchased 6 books. 3 being from ora.com and still nothing on
textboxes and database. Can anyone else help?
Look for these functions on php.net for information:

pg_pconnect()
pg_query()
pg_fetch_assoc()
pg_escape_string()
htmlspecialchars()
urlencode()

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
password=bar');
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
id='.intval($_POST['id']).';';
$res = pg_query($dcn, $sql);
if ($res === false) die('update error');
break;
}
}

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

-Robert
Michael Vilain wrote:
In article <11**********************@75g2000cwc.googlegroups. com>,
"fjm67" <fj*********@yahoo.comwrote:
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
grateful.
>
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.
>
Frank
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 (http://www.ora.com), 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...
Jul 16 '06 #3

P: n/a

"rlee0001" <ro*********@hotmail.comwrote in message
news:11**********************@35g2000cwc.googlegro ups.com...
>
fjm67 wrote:
>I have purchased 6 books. 3 being from ora.com and still nothing on
textboxes and database. Can anyone else help?

Look for these functions on php.net for information:

pg_pconnect()
pg_query()
pg_fetch_assoc()
pg_escape_string()
htmlspecialchars()
urlencode()

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

http://www.tonymarston.net
http://www.radicore.org
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
password=bar');
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
id='.intval($_POST['id']).';';
$res = pg_query($dcn, $sql);
if ($res === false) die('update error');
break;
}
}

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

-Robert
>Michael Vilain wrote:
In article <11**********************@75g2000cwc.googlegroups. com>,
"fjm67" <fj*********@yahoo.comwrote:

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

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.

Frank

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 (http://www.ora.com), 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...

Jul 17 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.