473,396 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

HTML forms and Postgres

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
3 4471
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

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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: N.K. | last post by:
Hi, I've just installed postgres on the Linux server. It is supposed to start automatically which I think it does since I can run an sql stmt right away. When I'm trying to connect from a remote...
7
by: Abdul-Wahid Paterson | last post by:
Hi, I have had a site working for the last 2 years and have had no problems until at the weekend I replace my database server with a newer one. The database migration went like a dream and I had...
15
by: Birahim FALL | last post by:
Hi, I'm very fresh to PostgreSQL, coming from Oracle. I want to developp web applications based on apache and postgresql. Is there an equivalent of OWA server (Oracle Web Application server) for...
18
by: Chris Travers | last post by:
Hi all; I have been looking into how to ensure that synchronous replication, etc. could best be implimented. To date, I see only two options: incorporate the replication code into the database...
3
by: warwick.poole | last post by:
I am interested in finding out about Enterprise scale Postgres installations and clustering, especially on Linux. Essentially I would like to know the possibility that Postgres can store the...
6
by: Prabu Subroto | last post by:
Dear my friends... Usually I use MySQL. Now I have to migrate my database from MySQL to Postgres. I have created a database successfully with "creatdb" and a user account successfully. But...
18
by: Joe Lester | last post by:
This thread was renamed. It used to be: "shared_buffers Question". The old thread kind of died out. I'm hoping to get some more direction by rephrasing the problem, along with some extra...
1
by: Jack Orenstein | last post by:
I'm trying to configure PHP 5.2.0 with support for Postgres 8.1.3. Postgres was installed with FC5 without source. PHP's configure needs source. When I run configure: configure: error: Cannot...
0
by: NM | last post by:
Hello, I've got a problem inserting binary objects into the postgres database. I have binary objects (e.g. images or smth else) of any size which I want to insert into the database. Funny is it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.