Dabrorius - WEB tutorials
Guestbook Part I [PHP tutorials]
Part 1 of this PHP tutorial will show you how to create a basic guestbook. Users will be able to leave comments on your page but you won't be able to delete or edit them
First you need to create new database.
You can do this using phpMyAdmin
If you have local server it's usually http://localhost/phpmyadmin
If you got hosting try accessing it from cpanel

Once there create new database let's name it "guest_db"
Now we need a new 5 colum table named "Comments" inside that database
Rows should be as following:

Id :: Type: Int :: Auto_increment :: Key
Comment :: Text
Name :: Text
Date : Text
Password :: Text

You can also just execute following php code

 
<?
sql = "CREATE TABLE `guest_db`.`Comments` (
`Id` INT NOT NULL AUTO_INCREMENT ,
`Comment` TEXT NOT NULL ,
`Name` TEXT NOT NULL ,
`Date` TEXT NOT NULL ,
`Password` TEXT NOT NULL ,
PRIMARY KEY ( `Id` )
) ENGINE = MYISAM ";
 
mysql_query($sql);
?>
 


Your table structure should look like this:
table structure

Now create a new PHP file and call it index.php
First we need to connect to our database.
You will need to know your database host, username and password.

 
//name of your host, usually it' localhost
$dbhost = 'localhost'; 
 
//your database username, it's usually root for local server 
//or your hosting username 
$dbuser = 'root'; 
 
//your database password, usually it's your hosting password
$dbpass = ''; 
 
//we tell PHP to connect with given information
$conn = mysql_connect($dbhost, $dbuser, $dbpass) 
or die ('Error connecting to mysql'); 
 
//once connected to database server, we must select databasee
$dbname = 'guest_db'; 
 
//and connect to it :)
mysql_select_db($dbname); 
 


Then we check if script received a new comment;

 
//recieve data that was posted by form element named "comment" and "name"
$comment = $_POST['comment']; 
$name = $_POST['name'];
 
//generate current date in format "month/date/year"
$date = date("m/d/y"); 
 
if($comment && $name) //if both comment and name were received do following
{
	$sql = "INSERT INTO `Comments` VALUES ('','$comment','$name','$date','')";
	mysql_query($sql);
}
 

This part is simple, we create new row in table comments and fill it with our data
Colum Id is set to 'auto_increment' so it will be genereted atumaticly
Colum Password is left blank since we won't use it until part 2 of this tutorial

Now we show all comments that are already posted

 
$sql = "SELECT * FROM `Comments` WHERE 1 ORDER BY `Id` DESC";
 


First we send this query to database.
We ask database to give us some data, let's break this in few parts

SELECT * FROM `users` - we ask to give us some data from table users, between select and from we choose what columns we want to get, and * means we want ALL columns

WHERE 1 - in WHERE part we choose what rows we want to select (for example we could choose all rows that have Id 5, or all rows that have Name = John) but this time we want ALL rows (since we want to show all comments) and 1 is always true so it will select all rows

ORDER BY `Id` DESC - we want selected data to be ordered by Id descending

 
$result = mysql_query($sql);
 

We execute command and store received data in variable $result.
So let's assume that you know how while loop, echo and arrays work.
(it's some basic programming so try to learn this :) )

 
while( $row = mysql_fetch_array($result) )
{
 

mysql_fetch_array will return one row, stored as array and then move to next row,
So next time it's called in $row will be stored 2nd array the 3rd and so on...
Now we have our data we just need to put it on screen

 
$row['Comment'] = nl2br($row['Comment']); 
 


once stored in database new line sings are same as in string (\n)
but we must convert those sings to
so we can show it in html properly

 
echo $row['Comment']."<br> <i> By: ".$row['Name']." on ".
$row['Date']."</i><br>-----------------------<br><br>";
}
 


this is just HTML form used to input data
just make sure that you replace index.php with whatever this file is called on your server

 
echo" 
<form name='Form1' method='post' action='index.php'>
<textarea name='comment'>
your comment here
<//textarea>
<br>
<input name='name' value='your name here'>
<br>
<input type='submit'>
</form>
";
 


And that's it!
Here is whole code in one piece

 
<?
//PART 1 ----------------------------
$dbhost = 'localhost'; 
$dbuser = 'root'; 
$dbpass = ''; 
$conn = mysql_connect($dbhost, $dbuser, $dbpass) 
or die ('Error connecting to mysql'); 
$dbname = 'guest_db';
mysql_select_db($dbname); 
 
//PART 2 ----------------------------------
$comment = $_POST['comment']; 
$name = $_POST['name'];
$date = date("m/d/y"); 
 
if($comment && $name) 
{
	$sql = "INSERT INTO `Comments` VALUES ('','$comment','$name','$date','')";
	mysql_query($sql);
}
 
//PART 3 -----------------------------------------
$sql = "SELECT * FROM `Comments` WHERE 1 ORDER BY `Id` DESC";
$result = mysql_query($sql);
 
while( $row = mysql_fetch_array($result) )
{
	$row['Comment'] = nl2br($row['Comment']); 
	echo $row['Comment']."<br> <i> By: ".$row['Name']." on "
	.$row['Date']."</i>";
	echo "<br>-----------------------<br><br>";
}
 
//PART 4 ------------------------------
echo" 
<form name='Form1' method='post' action='index.php'>
<textarea name='comment'>
your comment here
<//textarea>
<br>
<input name='name' value='your name here'>
<br>
<input type='submit'>
</form>
";
 
?>