Complete MySQL Guestbook Code

To skip the learning part and just get the guestbook script, read on.

First we'll create an include file for the connection info, which will save us from having it seperately in each script. Paste the following into a file called "config.php"

<?php
$server = "localhost";  // server to connect to.
$database = "mydata";  // the name of the database.
$db_user = "myusername";  // mysql username to access the database with.
$db_pass = "mypassword";  // mysql password to access the database with.
$table = "guestbook";  // database table
$rows = 20;    // the number of table rows to display
?>
You'll need to create the guestbook table on your database, to store the info in. To do this, paste the following into a file called "create.php". Then upload both create.php and config.php, and point your browser to create.php.
<?php

include "config.php";

// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());

// create tables on database
$create = "create table $table (
id int(11) NOT NULL auto_increment,
name char(30) NOT NULL,
email char(80) NOT NULL,
comment char(250) NOT NULL,
primary key (id)
);";

mysql_query($create)
or die ("Could not create tables because ".mysql_error());

echo "Success! The tables have been created.";
?>
Now you'll need to create your guestbook entry form. This can be a regular HTML page that simply points to your PHP script. It should look like this:
<form action="form.php" method="post">
<table><tr>
<td>Name: </td>
<td><input type="text" name="name" size="15" maxlength="30"></td>
</tr><tr>
<td>Email: </td>

<td><input type="text" name="email" size="15" maxlength="80"></td>
</tr><tr> 
<td>Comments: </td>
<td><textarea name="comment" cols="15" rows="4"></textarea></td>
</tr><tr>

<td colspan="2" align="center"><input type="submit" name="submit" value="Submit">
<input type="reset" name="reset" value="Reset"></td>
</tr></table>
</form>
Then, create a file called form.php, or whatever you pointed the form to, and put this code in it:
<?php

include "config.php";

// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());

// insert data into database
$insert = mysql_query("insert into $table values ('NULL', '$name', '$email', '$comment')", $link)
or die("Could not insert data because ".mysql_error());

mysql_close();

echo "Thanks for signing my guestbook! Click <a href=read.php>here</a> to view your entry.";

?>
Now we create read.php, which is the page that displays the guestbook entries:
<?php

include "config.php";

// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());

// read data from database
$result = mysql_query("select * from $table order by id desc limit $rows", $link)
or die ("Could not read data because ".mysql_error());

// print the data in a table
if (mysql_num_rows($result)) {
print "\n";
while ($qry = mysql_fetch_array($result)) {
print "$qry[name]: ";
print $qry[comment];
print "\n";
}
print "</table>\n";
}

mysql_close();

?>
That's it! If you're still unsure, you can download the files directly here. Please note that this is a very simple script, and is intended only as an example of MySQL functions. It has no precautions against spammers, etc.