/* connect to the database */
mysql_pconnect("$host","$user","$password") or die("Can't connect to the SQL-server");
mysql_select_db("$database");
/* action=view : retrieve data from the database and show it to the user */
if($action == "view") {
/* function for showing the data */
function search_it($name) {
/* some vars */
global $offset,$total,$lpp,$dir,$records;
global $table,$html_mail,$admin_name,$admin_mail;
/* select the data to get out of the database */
$query = "SELECT * FROM $table ORDER BY ID DESC";
$result = mysql_query($query);
$total= mysql_numrows($result);
print "
(Add your name)
";
if ($total== 0) {
print "At the moment there are no persons in the guestbook.
"; }
elseif ($total> 0) {
/* default */
$counter=0;
if ($dir=="") $dir="Next";
$lpp = $records;
if ($offset==0) $offset=0;
if ($dir=="Next") {
if ($total > $lpp) {
$counter=$offset;
$offset+=$lpp;
$num=$offset;
if ($num > $total) {
$num=$total; } }
else {
$num=$total; } }
elseif ($dir=="Previous") {
if ($total > $lpp) {
$offset-=$lpp;
if ($offset < 0) {
$offset=0; }
$counter=$offset-$lpp;
if ($counter < 0)
$counter=0;
$num=$counter+$lpp; }
else {
$num=$total; } }
while ($counter < $num) {
$j=0;
$j=$counter + 1;
/* now really grab the data */
$i1=mysql_result($result,$counter,"name");
$i2=mysql_result($result,$counter,"email");
$i3=mysql_result($result,$counter,"job");
$i4=mysql_result($result,$counter,"comments");
$i4 = stripslashes ("$i4");
/* print it in a nice layout */
print "\n";
print "\n";
print " \n";
print "Name: $i1\n";
print " Job: $i3\n";
print " Comment:\n";
print " $i4\n";
print " |
\n";
print "\n";
$counter++;
}
}
mysql_close();
}
/* execute the function */
search_it($name);
/* See if we need to put on the NEXT or PREVIOUS buttons */
if ($total > $lpp) {
echo("");
}
}
/* action=add : show a form where the user can enter data to add to the database */
elseif($action == "add") { ?>
}
/* action=send : add the data from the user into the database */
elseif($action == "send") {
$reset = reset($required);
while (list($key, $value) = each($required)) {
if($form[$value] == "") {
$count++;
}
}
if($count > 0) {
?>
}
else {
if(!$form[email]) {
$form[email] = "n/a"; }
if(!$form[subject]) {
$form[subject] = "n/a"; }
if(!$form[name]) {
$form[name] = "n/a"; }
/* check if a HTML-mail should be send or a plain/text mail */
if($html_mail == "yes") {
mail("$admin_name <$admin_mail>","PHP3 Guestbook Addition","$form[name] ($form[email]) wrote the following message in the guestbook :
| |
$form[comments] | |
| |
Name: | $form[name] |
Beroep: | $form[job] |
Email: | $form[email] |
", "From: $form[name] <$form[email]>\nReply-To: $form[name] <$form[email]>\nContent-type: text/html\nX-Mailer: PHP/" . phpversion());
}
else {
mail("$admin_mail","PHP3 Guestbook Addition","$form[name] ($form[email]) wrote the following message in the guestbook : \n\n $form[comments]\n\n Name: $form[name] \n Job: $form[job] \n Email: $form[email] \n", "From: $form[name] <$form[email]>\nReply-To: $form[name] <$form[email]>\nContent-type: text/plain\nX-Mailer: PHP/" . phpversion());
}
/* MySQL really hates it when you try to put things with ' or " characters into a database, so strip these...*/
$form[comments] = addslashes ("$form[comments]");
$query = "INSERT INTO $table VALUES('','$form[name]', '$form[email]', '$form[job]', '$form[comments]')";
$result = MYSQL_QUERY($query);
?>
Thank you, , for adding your name to our guestbook.
View your comment now.
}
}
/* if there's no action given, then we must show the main page */
else {
/* get the number of entries written into the guestbook*/
$query = "SELECT name from $table";
$result = MYSQL_QUERY($query);
$number = MYSQL_NUMROWS($result);
if ($number == "") {
$entry = "At the moment there are no persons"; }
elseif ($number == "1") {
$entry = "At the moment there is 1 person"; }
else {
$entry = "At the moment there are $number persons"; }
echo "
";
echo "$entry in the guestbook.
";
echo "
";
if ($number > "") {
echo ""; }
echo "";
}
?>
Script by Advanced IT Services Holland