//this is a very simple guestbook example, i found that tere are not that many
//postgreSQL examples out there for the very beginners, so here is one
//it does not limit number of records per page, it does not give ou administration
//features(for this i have another script - maybe i'll post it here as well later)
//the table is:
/*CREATE TABLE gb_record (
id int4,
name varchar,
email varchar,
location varchar,
comments varchar,
url varchar
)
;
*/
//we do want a proper header for our file, don't we?!
echo "Guestbook Example ";
// connect to the database, since each part of this uses
//the connection, we can just connect once at the beginning
$connection=pg_connect("host=yourhos dbname=dbname user=user password=****
port=5432");
if (!$connection)
{
echo "Could not connect to the database";
exit;
}
// if the query string (guestbook.php3?stuff) is add, then present the form to add entries
if ($argv[0] == "add"):
?>
Please take a moment to share your comments with us.
// if the query string is view, the fetch the guestbook entries
elseif ($argv[0] == "view"):
echo "View Guestbook Entries
";
$sql='select name, email,url,location, comments from gb_record order by id desc';
// get stuff from the database
// $result = mysql_query( "select name, email, url, job, location, comments
from guestbook");
$result=pg_exec($connection, $sql);
if (!$result)
{
echo "Got no results";
exit;
}
// fetch the rows one at atime, and then echo the data to the page
//we also want to know the number of the records in the database, so that we could
//display the exact number, and it didn't give us a warning: can not get to the next
//record message
$r1 = pg_exec($connection, "select max(id) from gb_record");
$row1 = pg_fetch_row($r1,0);
$num1 = $row1[0];
pg_freeresult(r1);
$r=0;
while ($r<$num1)
{
$row = pg_fetch_array($result,$r);
echo "
";
//we could have these fields in the echo already, but it does not access
// array elements correctly from within ""
$mto=$row[1];
$nam=$row[0];
$urrl=$row[2];
$loc=$row[3];
echo "
Message by: HREF="mailto:$mto\">$nam";
echo "
Web Page: $urrl";
echo "
From: $loc";
echo "
Comments:";
echo "
";
echo $row[4];
$r++;
}
echo "
Add an entry to the
guestbook
";
pg_freeresult($result);
// if we're submitting a guestbook entry
elseif (isset($cmd) && $cmd == "send"):
// mail guestbook entry to the webmaster
mail ("your@email.here", "guestbook entry", "Comment: $comments\nURL: $url
\nLocation: $location \n", "From: $name <$email>");
// postgreSQL really hates it when you try to put things with ' or "
// characters into a database
$result = pg_exec($connection, "select max(id) from gb_record");
$row = pg_fetch_row($result,0);
$num = $row[0];
$num++;
$comments = addslashes("$comments");
pg_freeresult($result);
//we want to increment the id by 1
pg_exec($connection, "insert into gb_record values
($num,'$name', '$email', '$location', '$comments', '$url');");
// insert the data into the database
?>
Thank you!
We really appreciate your cooperation, to continue
browsing the web-site
click here.
else:
// lastly, we must be at the main page. Get the number of entries in the guestbook
//so we can tell the visitor how many there are
$result = pg_exec($connection, "select max(id) from gb_record");
$row = pg_fetch_row($result,0);
$num = $row[0];
if ($num == "")
{
$entry = "There are currently no entries";
}
elseif ($num == "1")
{
$entry = "There is currently 1 entry";
}
else {
$entry = "There are currently $num entries";
}
echo "Welcome to the Count Zero guestbook.
$entry in the
guestbook.
";
echo "Add an entry to the
guestbook
";
echo "View entries in the
guestbook
";
endif;
pg_freeresult($result);
pg_close($connection);
?>