Php Programming Code Examples
Php > Databases Code Examples
A very simple postgreSQL guestbook script
A very simple postgreSQL guestbook script
//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 "<HTML><HEAD><TITLE>Guestbook Example</TITLE></HEAD><BODY>";
// 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"):
?>
<P>Please take a moment to share your comments with us.</p>
<P>
<FORM NAME="guestbook" ACTION="<?echo $PHP_SELF?>" METHOD=POST>
<INPUT TYPE=hidden NAME=cmd VALUE=send>
Your Name: <INPUT TYPE=text NAME=name>
<BR>Your E-mail address: <INPUT TYPE=text NAME=email>
<BR>Your Web Page address: <INPUT TYPE=text NAME=url>
<BR>Where are you from: <INPUT TYPE=text NAME=location>
<BR>Comments:
<BR><TEXTAREA NAME=comments COLS=60 ROWS=6></TEXTAREA>
<CENTER><INPUT TYPE=submit VALUE=Submit><INPUT TYPE=reset
VALUE=Clear></CENTER>
</FORM>
<?
// if the query string is view, the fetch the guestbook entries
elseif ($argv[0] == "view"):
echo "<H2>View Guestbook Entries</H2>";
$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 "<HR>";
//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 "<p align=justify><BR><B>Message by:</B> <A
HREF="mailto:$mto\">$nam</A>";
echo "<BR><B>Web Page:</B> <A HREF=\"$urrl\">$urrl</A>";
echo "<BR><B>From:</B> $loc";
echo "<BR><B>Comments:</B>";
echo "<BR></p>";
echo $row[4];
$r++;
}
echo "<br><H3><A HREF=\"$PHP_SELF?add\">Add an entry to the
guestbook</A></H3>";
pg_freeresult($result);
// if we're submitting a guestbook entry
elseif (isset($cmd) && $cmd == "send"):
// mail guestbook entry to the webmaster
mail ("[email protected]", "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
?>
<h1>Thank you!</h1><br>We really appreciate your cooperation, to continue
browsing the web-site
<a href="index.html">click here</a>.<br>
<?
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 "<h1>Welcome to the <I>Count Zero</I> guestbook.<br> $entry in the
guestbook.</h1>";
echo "<H3><A HREF=\"$PHP_SELF?add\">Add an entry to the
guestbook</A></H3>";
echo "<H3><A HREF=\"$PHP_SELF?view\">View entries in the
guestbook</A></H3>";
endif;
pg_freeresult($result);
pg_close($connection);
?>
</BODY>
</HTML>