|
Posted by rlee0001 on 07/16/06 12:46
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 <1152970930.914425.110350@75g2000cwc.googlegroups.com>,
> > "fjm67" <fjmannarino@yahoo.com> wrote:
> >
> > > 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...
[Back to original message]
|