WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search


Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - השוואת מחירים בסופר
ZeroLag.com
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : A simple guestbook application demonstrating using MySQL and PHP3 to generate interactive web pages
Categories : Complete Programs, MySQL, PHP, Databases Update Picture
Chad Cunningham
Date : Nov 29th 1998
Grade : 3 of 5 (graded 37 times)
Viewed : 112553
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Chad Cunningham
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

<?
/*

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

There's a functional demo of this at http://socrates.mps.ohio-state.edu/chad/guestbook.php3

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;
?>




bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager
Categories : MySQL, PHP, MySQL, Complete Programs, Databases
This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server.
Categories : Databases, MySQL, Complete Programs, PHP, Databases
phpMyAdmin is a free software tool written in PHP intended to handle the administration of MySQL over the World Wide Web.
Categories : Databases, MySQL, Complete Programs, PHP
This program will take data from a user via a web based form, validate it, show it to the user for re-validation, and finally insert it into the database. Plenty of sanity checking on the fields in the form.
Categories : MySQL, HTML and PHP, PHP, Complete Programs, Databases
Shopping Cart e-Commerce Solution
Categories : Complete Programs, PHP, MySQL, Databases
Create and restore backup of MySQL databases
Categories : MySQL, Databases, PHP, PHP Classes, Complete Programs
AITSH Download
Categories : PHP, Complete Programs, MySQL, Databases
Tropicalm Genetree Family (MySQL based family tree)
Categories : PHP, Interfaces, Databases, MySQL, Complete Programs
DDN FFA Network Script
Categories : PHP, MySQL, Complete Programs, HTML and PHP, Databases
Web Self Service Resource Scheduler Using Session Variables under php4 includes Calendar building code - requires MySQL
Categories : PHP, Complete Programs, Calendar, MySQL, Databases
DirtSearch Version 3.5 full function robust PHP and MySQL (and other databases) Site or Web Wide Search Engine
Categories : PHP, MySQL, Complete Programs, Search, Databases
Browse a MySQL database & draw a tree view & load final items into a template page.
Categories : MySQL, Complete Programs, Algorithms, PHP, Databases
Education Center is a set of PHP-scripts to administer a corporate education and examination system via Internet/intranet written in PHP for MySQL.
Categories : PHP, Databases, MySQL, Complete Programs
myCSV-dump converts a simple CSV-flatfile-database into an MySQL-dump.
Categories : PHP, MySQL, Databases, Complete Programs
Shopping Basket On-Line Ordering System.
Categories : Complete Programs, MySQL, PHP, Ecommerce, Databases
 Pierre Verbakel wrote : 20
Great stuff Chad,

I have got the retrieval part working. For the add-entry part I am anxious to know how the file 
/contact/index.htm looks like. Can you supply this as well?

Regards,

Pierre Verbakel
 
 Jos van Uden wrote : 26
Hi Chad,

I was just looking for an example on how php 
communicates with mysql. I still learning this stuff. 
Your guestbook has made it a lot clearer.

Thanks,

Jos

 
 Jonathan Sharp wrote : 54
FINALLY! A simple MySQL php script! The only MySQL 
PHP scipts I can find, have a load of extra stuff builtin 
which I don`t want. And every attemp to modify the 
program resulted in errors...but I was able to see how 
PHP and MySQL interact and my script now worked on 
the first attempt! THANKS!

P.S. We had origionally had a Perl and flatfile db but lag 
times were starting to get so big that we have changed 
to mysql and it`s blazin` fast now!
 
 Jano Gege wrote : 63
I wont to read it.
 
 Joeker nie wrote : 85
   the guestbook  is only what i looking for.through 
seeing the source i learn mang from it. thank you for
your guestbook! *_*

 foolboy 
 
 Alex Kamumba wrote : 87
Was ist PHP ?! guestbook ?
 
 Dave Whyte wrote : 88
Wouldn`t it be lovely to be able to do a php guestbook without having to complicate it with SQL? is this even
possible? I aim to find out :)
 
 Boaz Yahav wrote : 134
No problem...
The database is simply the easiest way to save data 
but a simple file would due...

berber
http://www.WeberDev.com
 
 Dylan Hall wrote : 137
thanks chad, BIG help :)
 
 Paul G wrote : 139
PHP looks pretty amazing, but the database stuff looks 
pretty complicated.  I`ve been using www-sql to connect 
my web pages to MySQL databases and it works great 
and is very simple.  Much simpler than PHP anyway.  But 
there are some things that cannot be done with www-
sql.  It looks like you can do pretty much anything with 
PHP!
 
 Chad Cunningham wrote : 150

I guess I should probably mention that since so many people viewed this, I wrote a real guestbook that`s actually usable rather than just an unfeatured demo. It`s also a better example of how to properly interact with a database in an app.

See it at http://socrates.math.ohio-state.edu/chad/guestbook/
 
 Murat Balaban wrote : 152
cool, i`m learning php!
 
 Dozehead wrote : 156
Hello,

I`m trying to find a good method for building a website 
based on some database-stuff without having to pay 
lots af extra to my ISP, your demo of PHP and MySQL 
might just help me out!
Thanks a lot and keep up the good work.

Doze..:-)
 
 Gert Jrgensen wrote : 157
Hi, This is a cool exampel which I learned alot of... 


Thanks..
 
 cipson cit wrote : 166
please give me some php documentation through e mail
by
[email protected]
 
 Michel van Westen wrote : 167
Hi there, excuse me for my English but im Dutch, i was 
busy making a guestbook for myself but it just didnt 
work out `till u ren into this guestbook. I just rebuilded 
yours here and there. And i think its becoming a pretty 
cool guestbook check out my version at 
dekas.dynip.com/guest/guest.php3 for the source code 
you can mail me
 
 Michel van Westen wrote : 170
Sorry i have to bother you guys again but can anyone 
tell me how i can view the last message first? Im 
breaking my head over it now for two days. I would 
very very appreciate it. 
Tnx in advance
Michel 
 
 Thanh Liem Tran wrote : 171
Hi, 
I really appreciate your work, im a beginner in PHP3 so, 
I come here to learn more about this language.
 
 Metin A. wrote : 195
To get the last messages first without changing the database table the 
only thing you need to change is the following line in the code:

x----------------------------------------------------------------------------------------------------------------------------------------------------------
 // get stuff from the database    
     $result  =  mysql_query(   "select  name,  email,  url,  job,  location, comments, id from  guestbook order by id DESC");
X----------------------------------------------------------------------------------------------------------------------------------------------------------

A better way of doing things would be to store the submission time in the database table and then sort after that.
Then you kan also show exactly when the post was made.

 
 Metin A. wrote : 196
By the way there is already a free and great guest book "MyGuestbook" which
supports different languages. Check it out or get it @ "http://mygb.php-homepage.de"
 
 Sean Meese wrote : 203
This guestbook is very simple and easy to implement.  
I`ve got my own php/mysql guestbook, not based on 
this one, that I think is much nicer.  Come check it out.  
If you like it email me for the source code.

http://www.strudleman.com/guest/guestbook.php3

Strudleman
 
 Andres Sheh wrote : 216
Great script.  Thanx.  I have learnt a lot.  

Although, I still have to figure out why your link to the 
new version didn`t appear.

Check out the new script  http://socrates.mps.ohio-
state.edu/chad/guestbook/index.php3

Andres
 
 Mazalaigue Vincent wrote : 219
why dont you use mail() fonction in your script ?
It`s seems me easier ...
Your script is realy cear 
thanks

Vince
 
 Yeah Cao wrote : 238
Hi, I don` know how to send mail when I am programing 
in Win98.

any one who can tell me how?
 
 Jacques JOCELYN wrote : 242
I find this guest book very useful (when it works) :-))

Did someone succed by putting this script on his web 
site ?
Cause for me it`s impossible !
the funtion :
...
  echo "&lt;a href=\"$PHP_SELF?action=view&view=
$num\"&gt;Previous $show comments&lt;/a&gt;\n";
    }

doesn`t work ! When the webpage is generated : this is 
the link php returns:
http://jacques.jocelyn.free.fr/jacques.jocelyn.free.fr/gues
tbook/index.php3?

instead of 
http://jacques.jocelyn.free.fr/guestbook/index.php3?

DId I make something wrong ?
Please help.
 
 Todd Boyle wrote : 245
this is good.  hmmmm. how does it work.... !
 
 Niall Kelly wrote : 258
Nice script !
 
 honny sonng wrote : 272
like u r php3 guestbook!

^-^  Thanks . 

I`d like to make some friend . try to learning pHp3 .

to build web site . my site address is 
icnet.home.chinaren.com

honny
 
 Steven Perry wrote : 298
I`d like to see how you displayed the messages 10 on 
each page.  A code sample would be very useful.
 
 Bronislava Sinkova wrote : 304
Fatal error: Call to unsupported or undefined function 
mysql_connect() in 
/domains/miesto/bronka/www_root/guestbook.php3 on
line 59
 
 Michel van Westen wrote : 329
Thanks so much for the desc query i was not that good 
in SQL im learning it now. My guestbook is totally 
finished thanks to A.Metin. See the result at 
http://www.dekas.michel.com and click view or sign 
from the menu. Thanks everybody who helped me out
 
 Earl Levy wrote : 345
Hey, Thanks alot for this example....it really helped 
alot.....But, (still some questions)..If I`m using three 
different tables in a db that are all referenced by 
Primary (id)s...how do I add info to all tables from the 
same input screen.....using mysql_query...w/  an insert 
for each table?  or should I use a SUBMIT button after 
each table`s info in keyed in? (hope u understand my 
question/delimiter..&lt;=computer word for dilemma  :-)
 
 Earl Levy wrote : 346
Can anyone tell me where  the $argv[0] (argument 
variable) is assigned to "view" or "add"  so that 
everything flows smoothly?   Thanks for the responses 
in advance.   P.S. Is there some other code missing? 
(maybe some HTML stuff?)  
 
 Earl Levy wrote : 356
I took some time and looked closely at this example and 
have answered all of my questions....This was really 
helpful in understanding how php, sql, & html all work 
together....Thanks especially for the color schemes also. 
They too helped alot .........
 
 James Worthington wrote : 386
Thanks God ! I thought I`d never figure out how to do an insert into mysql with PHP. Low and behold, you 
use mysql_query. I only thought of query as SELECT. It must do UPDATE, DELETE, ALTER, practically 
everything. I`m home free ! Thanks ever so much!
 
 Bill Coker wrote : 503
I created the guestbook db ok. But i`m getting an error during the connection.

----------------------------------------------------
Warning: MySQL Connection Failed: Access denied for user: `I removed this for security` (Using password: YES) in /home/httpd/html/guestbook.php3 on line 53
Error opening database
------------------------------------------------------------------------

It`s seems it not accepting the password. I did not  give it one when I created it. It did not ask me for one either.

I tried using my root usr/pass but that did not work either. I tried not using a pass at all. That did not work either.

Why does it say using password "YES".

Could someone give me an idea of what it is that i`m doing wrong.

Thanks alot.

Bill.
 
 jeroen van der wrote : 564
Hi all,

Please be aware that it is very risky to use this guestbook on your website. 
If you decide to use it you should make sure to validate user input first before you write any data to your database! For example: you don`t want to except javascript  as `comments` and you want a valid e-mailadres.
Never trust input from the internet!
For output you can use php functions to covert ACSII to HTML and linebreaks (\n) to &lt;br&gt; tags. 

Regards,
Jeroen
 
 boby bob wrote : 732
Hi, 
I`m boby from Russia.
Sorry to my bad English.
But I`d like to say - this site
is the best what I can see.
So much examples.
It`s beautiful.

 
 sjc sjc wrote :1053
I`m getting errors please assist


Notice: Undefined variable: argv in C:\Data\web sites\James Family Site\test.php on line 70

Notice: Undefined variable: argv in C:\Data\web sites\James Family Site\test.php on line 88

Welcome to the Calculus&Mathematica guestbook. There are currently no entries in the guestbook.
Notice: Undefined variable: PHP_SELF in C:\Data\web sites\James Family Site\test.php on line 148


Add an entry to the guestbook

Notice: Undefined variable: PHP_SELF in C:\Data\web sites\James Family Site\test.php on line 149

View entries in the guestbook