OxyScripts.com
Menu spacer Home Tutorials Articles Code Forums irc.freenode.net #oxyscripts
Main (PHP)
Home Forums PHP News PHP Tutorials Articles PHP Code Snippets Contact Us Sysadmin Resources Books Template Shop
3rd Party Streams
SlashDot PHPDeveloper.org PHP.Net
Resources
PHP Manual MySQL Manual Smarty Manual PEAR Manual PHP-GTK Manual Symfony Manual
Code Snippets
Authentication Database Graphics HTTP Miscellaneous Time/Date
Affiliates
Scripts TutorialMan TutorialGuide CodingForums.com PHP Scripts Cheap Web Hosting Affordable Web Hosting

Search This Site :     PHP Function Reference :
 

PHP / mySQL Guestbook

By Chad (2002-06-14. 80956 views.)
This tutorial shows you everything you need to know to make your own PHP guestbook !

This tutorial will teach you how to make a very simple MySQL guestbook that will keep track of all the posts.



You will need PHP and MySQL on your server for this to work.



Ok, so here we go. First you need to open up a text program like NotePad or vim and enter the following into the blank page:

<?php

<?php
$PostPage 
"post.php"//Where people will sign the guestbook
$gbPage "guest.php";  //Where people will view the guestbook

$allowHTML 0;    //Change this if you want people to post HTML in posts   1 = Yes, 0 = No

$notify 0//When this is set, you will be emailed every time your guestbook is signed   1 = yes, 0 = No
$my_email "youremail@emailhost.com"//Enter your email address here
$subject "New Guestbook Entry" //This is the subject of the notification email


$username "yourname"//Database Username
$password "yourpassword"//Database password
$db_name =  "name_db"//Database name

$limit 6//This is the number of entries that will be displayed per page
?>

?>



Change all the vars that need to be changed in that file and Save this file now.


You must save it as "config.inc.php"



If you change this name, you will have to change it in the other files (explained in the following pages). That's all for the first part of the tutorial. You now have a config file for your upcoming MySQL database! Click next !

This is part II. By now you should have a config file for your guestbook.



This tutorial is really simple! It just tells you what to place in your MySQL database to create the proper tables.



All you need to do is enter the following information into your MySQL database:

<?php

CREATE TABLE guestbook 
(
   
name varchar(50),
   
date varchar(40),
   
email varchar(50),
   
comment longtext,
   
id int(4) DEFAULT '0' NOT NULL auto_increment,
   
PRIMARY KEY (id)
);

?>



What that does is creates a table called "guestbook". In that table, things like names, dates, emails, and comments will be stored. That's it! Told you it would be simple!



You should now have the following:


config.inc.php and a table called "guestbook" in your MySQL database. Let's continue !

You have now set up your config file and setup your database.


This bit will be a litle bit harder than the first two, but it's really simple. Copy, Paste, Edit...


Using a simple HTML editor, you can change everything from the page background color, to link names, etc..


Change "http://yourhost/yourname/mainnpage.html" to a link back to your main page. You can change how the form is displayed, but be sure to keep the input NAME values as they are.


Just copy and paste this code into a blank page:

<?php

<html>
<
head>
<
title>Your Guestbook</title>
</
head>
<
BODY bgcolor="#FFFFFF">
<
a href="http://yourhost/yourname/mainnpage.html"><font size="2">Click 
      here to go back to the main page
</font></a>
<
table width="100%" border="0" cellspacing="0" cellpadding="0" align="LEFT" valign="MIDDLE">
    <
tr>
        <
td align="LEFT" valign="TOP" width="85%">
<!-- 
begin main content -->
<
table>
<
tr><td rowspan="2" width="20"> </td></tr>
<
tr><td width="482" colspan="2">
<? 
// only change this include if you renamed the file
include ("config.inc.php");

echo 
$error1?>
<P>Submit a comment, or <a href="guest.php">view the guestbook</a> without making an entry</P>
          <P>Add a message to the guest book: 
            <FORM ACTION="guestbook.php" METHOD=POST>
              <P> Name:<BR>
                <INPUT TYPE=text NAME=name SIZE=30 maxlength=80>
                <BR>
                Email (Not Required):<BR>
                <INPUT TYPE=text NAME=email SIZE=30 maxlength=80>
                

                Comments: <BR>
                <t3xtarea name="comments" cols="30" rows="5" wrap="VIRTUAL"></t3xtarea>
                <BR>
              </P>
  <P><INPUT TYPE=submit NAME=gb VALUE="Add Message">
                  Please press only once: this may take a moment. 
            </FORM></P>
<p>

              

            </p>
            <p> </p>
            </td></tr>
</table>
<!-- end main content -->
</td>
</tr>
</table>
</body>
</html>

?>



Alright, that was easy! Copy, Paste, Edit! save this file up as "post.php". If you rename that file to something different, be sure to rename that in all the other files which we'll work on in the next few pages. You should now have the following done: A config file, a database that's all setup, and the form that the users will post on.

This is part IV. I hope you got the form all up and running, because that is needed for this part of the tutorial to do anything.
This part of the tutorial isn't hard, but it can get confusing if you plan on adding form fields or something...

<?php

<?php
// only change this include if you renamed the file
include ("config.inc.php");

// lets make sure they fill out the form before they submit
if (($name == "") AND ($email == "") AND ($comments == "")):

// This is the error message displayed...change this if you want...
    
$error1 "<font face='Arial' color='Red'><STRONG>Please fill out the form:</STRONG></font>

"
;    
    
    include(
$PostPage);

else:
if (
$allowHTML == 0):
$name ereg_replace("<","<",$name);
$name ereg_replace(">",">",$name);
$email ereg_replace("<","<",$email);
$email ereg_replace(">",">",$email);
$url ereg_replace("<","<",$url);
$url ereg_replace(">",">",$url);
$urltitle ereg_replace("<","<",$urltitle);
$urltitle ereg_replace(">",">",$urltitle);
$referral ereg_replace("<","<",$referral);
$referral ereg_replace(">",">",$referral);
$comments ereg_replace("<","<",$comments);
$comments ereg_replace(">",">",$comments);

endif;

//connect to the database

// change localhost to your db host name
$mysql_link mysql_pconnect"localhost""$username""$password"// Enter your database info here
               
or die( "Unable to connect to server"); 
mysql_select_db"$db_name") or die( "Unable to select database"); 

$insert "INSERT INTO guestbook(name, date, email, comment) VALUES('$name', SYSDATE(), '$email', '$comments')";
$mysql_insert mysql_query($insert$mysql_link)
    or die(
"please notify $my_email that the script is not inserting entries<BR><BR>$name,
$email,
$comments

$insert"
);

// Email the new post

if ($notify == 1):

$comments ereg_replace("<BR>","n",$comments);

mail
(
"$my_email",
"guestbook entry",
"Name : $name
Email : $email
Note:

$commentsn
"
,
"From: $emailn"
);
endif;

//check for double entries

$name_chk $name;
$email_chk $email;
$comments_chk $comments;

include(
$gbPage);

endif;

?>

?>



This is really easy. All you have to do is open a blank page and paste all that above code into it.


Save the blank page up as "guestbook.php".

If you plan on adding fields to the form, be sure you connect to the proper tables in this file... if you just want to leave the form as it is, you don't need to change anything. Alright, so now you have your config file, your database setup, your form that users will post on, and the functions of that form all in this file.


The only thing left for you do do is get the page where the database information will be displayed.

This is the final part of the tutorial. You should have everything set up and working, but you're missing one of the most important files! The people won't be able to see their own posts!

Just like back in page 3, you can open this in a HTML editor and change things like the background color, links, etc..

Change "http://yourhost/yourname/mainnpage.html" to a link back to your main page.

Very simple. Once again, Copy, Paste, Edit...

Make sure you don't edit any of the database stuff though unless you know what you're doing!

<?php

<html>
<
head>
<
title>Your Guestbook</title>
</
head>
<
BODY text="#000000" bgcolor="#FFFFFF">
<
table width="100%" border="0" cellspacing="0" cellpadding="0" align="LEFT" valign="MIDDLE">
<
tr>
<
td align="LEFT" valign="TOP" width="85%"
      <!-- 
begin main content -->
      <
a href="http://yourhost/yourname/mainnpage.html"><font size="2">Click 
      here to go back to the main page
</font></a>

      

      <
table>
<
tr><td rowspan="4" width="20"> </td></tr>
        <
tr
          <
td colspan="2"><a href="post.php">Post a message to this guestbook</a>

              

              </
td>
        </
tr>
<
tr><td>
<?
php
// only change this include if you renamed the file
include ("config.inc.php");

//connect
// change localhost to your db host name 
mysql_pconnect"localhost""$username""$password"
               or die( 
"Unable to connect to SQL server"); 
mysql_select_db"$db_name") or die( "Unable to select database"); 

    
$numresults=mysql_query("SELECT name, date, email, comment FROM guestbook");
    
$numrows=mysql_num_rows($numresults)
            or die (
"query 1 failed");
    
if (empty(
$offset)) {
    
$offset=0;
}

$query "SELECT name, date, email, comment FROM guestbook order by date DESC limit $offset,$limit";

$result mysql_query ($query)
    or die (
"query 2 failed");
        
while (
$row mysql_fetch_row ($result))
{
for (
$i 0$i mysql_num_fields ($result); $i++)
    {
    if (
$i 0)
        print (
"
"
);    
    if (
$i == 0){
        print 
"<b>Name: </b>";    
    }
    else if (
$i == 1){
        print 
"<b>Date: </b>";
    }
    else if (
$i == 2){
        print 
"<b>Email: </b>";
    }
    else{
        print 
"<b>Comment:</b>
"
;
    }
        
    print (
$row[$i]);
        
    }
print 
"

<center><hr></center>"
;
print (
"<P>");
}
if (
$offset >= 3) { 
$prevoffset $offset $limit
print 
"<a href="guest.php?offset=$prevoffset">PREV</a> n"

$pages=intval($numrows/$limit);
if (
$pages < ($numrows/$limit)){
$pages=($pages 1);
}
for (
$i 1$i <= $pages$i++) { 
$newoffset $limit*($i-1); 
if (
$newoffset == $offset) { 
print 
"$i n"
} else { 
print 
"<a href="guest.php?offset=$newoffset">$i</a> n"



//show next if not last
if (! ( ($offset/$limit) == ($pages 1) ) && ($pages != 1) ) { 
$newoffset $offset+$limit
print 
"<a href="guest.php?offset=$newoffset">NEXT</a><p>n"

?> 
</td></tr>
<tr><td colspan="2">


            <a href="post.php">Post a message to this guestbook</a></td>
        </tr>
</table>
<!-- end main content -->
</td>
</tr>
</table>
</body>
</html>

?>



So what this file does is connects to the database, and displays it all on a page. (All thanks to the file you made in part IV of this tutorial)

Really really easy... just copy and paste the code into a blank page.

Save the file as "guest.php"

Now you're all done with your MySQL guestbook! All the files should be uploaded into the same directory for this to work. Set the link to VIEW the guestbook to "guest.php" and the link to SIGN the guestbook to "post.php". If you plan on editing or adding fields in the guestbook, make sure you know what you're doing before you start! This guestbook is very flexible and can be modified to do pretty much anything form related! Like I always say, fool around with it!
 

 
   Print this page

Top Sponsor
Symantec\'s Norton SystemWorks 2006
Sponsors
CA
Sponsors
AdWords Dominator 125*125
Advertisting


Affiliates
VertexTemplates PHPFreaks CodeWalkers StarGeek DevScripts CGI & PHP Scripts PHP CMS Free Templates

Shopping Rebates   Sell It 4 You   Flash Page Counters   Get Insured
GPS Tracking Service   Charity Donate Info   Web Site Hosting   VOIP Service

Privacy Policy | Links | Site Map | Advertising

All content on OxyScripts.com is (©)2002-2007

 
Powered by Adrastea - Version 1.0.0. Copyright © Rune Solutions, 2004-2005