simple,guestbook,application,demonstrating,using,MySQL,and,PHP3

Mert 9/1/2016 0

A simple guestbook application demonstrating using MySQL and PHP3 to generate interactive web pages

PHP
<?    
/*   

I'm not a big fan of guestbooks, but when I did our website, my bosses wanted one. On the bright side,   
it make a great demonstration of using PHP and MySQL together. You could probably use about any SQL   
Server, with minor modifications.   

This guestbook is a bit limited in that   
-it doesn't have any management features, like editing/deleting entries   
-it doesn't limit how many entries are shown on one page   

The basic way this works is something like this:   
     -if the URL called is guestbook.php3?add, then we present the form to add an entry to the guestbook   
     -if the URL called is guestbook.php3?view, then we show all the entries in the guestbook   
     -if the page is passed the varaible cmd that has the value send, then we process the form data and add the entry 
to the database   
     -otherwise, we present the main welcome page   

Here's a dump of the database used by this:   

# MySQL dump 4.0   
#   
# Host: localhost    Database: guestbook   
#--------------------------------------------------------   

#   
# Table structure for table 'guestbook'   
#   
CREATE TABLE guestbook (   
  id mediumint(8) DEFAULT '0' NOT NULL auto_increment,   
  name varchar(30) DEFAULT '' NOT NULL,   
  email varchar(30) DEFAULT '' NOT NULL,   
  job varchar(30) DEFAULT '' NOT NULL,   
  location varchar(30) DEFAULT '' NOT NULL,   
  comments text DEFAULT '' NOT NULL,   
  url varchar(50),   
  PRIMARY KEY (id)   
);   

*/    
echo   "<HTML><HEAD><TITLE>Guestbook Example</TITLE></HEAD><BODY>";   
// path to mail server, we'll be mailing copies of entries to the admin to make 
//sure appropriate things are submitted   
$MP  =    "/usr/lib/sendmail  -t";     

// addresses to mail entries to 
$mail  =    "ccunning";        

// connect to the database, since each part of this uses 
//the connection, we can just connect once at the beginning    
mysql_connect(   "localhost",   "username",   "password");      

//select our database 
mysql_select_db(   "guestbook")  or  die(   "Error  opening  database");         

// 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.  If  you  have  a  specific  question  for  us,   
please  use  the  form  located  <A HREF="/contact/index.php3">here</A>.    
    <P><FRM  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>Your  occupation:  <INPUT  TYPE=text  NAME=job>    
    <BR>Where  you  call  home:  <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>";   

// get stuff from the database    
    $result  =  mysql_query(   "select  name,  email,  url,  job,  location,  comments from  guestbook");      

// fetch the rows one at atime, and then echo the data to the page    
    while  ($row  =  mysql_fetch_row($result))  {      
               echo    "<HR>";    
                echo    "<B>Name:</B>  $row[0]";    
              echo    "<BR><B>E-mail:</B>  <A  HREF=\"mailto:$row[1]\">$row[1]</A>";    
              echo    "<BR><B>Web  Page:</B>  <A  HREF=\"$row[2]\">$row[2]</A>";   
                echo    "<BR><B>Occupation:</B>  $row[3]";    
                echo    "<BR><B>From:</B>  $row[4]";    
                echo    "<BR><B>Comments:</B>";    
                echo    "<BR>$row[5]";   
    }   

// if we're submitting a guestbook entry    
elseif  (isset($cmd)  &&  $cmd  ==    "send"):     

// open a pipe to the mail server, andsend a copy to the admins, 
// the mail() function could also have been used   
            $fd  =  popen  ($MP,    "w");     
            fputs  ($fd,    "To:  $mail\n");   
            fputs  ($fd,    "Subject:  Guestbook  Addition\n");   
            fputs  ($fd,    "$name  ($email)  has  added  the  following  to  the  guestbook\n");   
            fputs  ($fd,    "Web  Page:  $url\n");   
            fputs  ($fd,    "Occupation:  $job\n");   
            fputs  ($fd,    "Location:  $location\n");   
            fputs  ($fd,    "$comments\n");   
            pclose  ($fd);   

// MySQL really hates it when you try to put things with ' or " 
// characters into a database   
            $comments  =  addslashes(   "$comments");    
            mysql_query(   "insert  into  guestbook  (name,  email,  url,  job, location,  comments)  values   

// insert the data into the database    
('$name',  '$email',  '$url',  '$job', '$location',  '$comments')");      
    ?>   
    <P>Thanks!  We  appreciate  your  comments  on  our  program.      
      <?    
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  =  mysql_query(   "select  max(id)  from  guestbook");    
            $row  =  mysql_fetch_row($result);    
           $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    "<P>Welcome  to  the  Calculus&<I>Mathematica</I>  guestbook.  $entry  in  the  guestbook.";    
    echo    "<H4><A  HREF=\"$PHP_SELF?add\">Add  an  entry  to  the  guestbook</A></H4>";    
    echo    "<H4><A  HREF=\"$PHP_SELF?view\">View  entries  in  the  guestbook</A></H4>";    
endif;    
?>
 

Report Bug

Please Login to Report Bug

Reported Bugs

Comments

Please Login to Comment

Comments