PHP Project-11 : To creata a simple guestbook by PHP

Posted: November 16, 2010 in HTML, PHP
Tags: ,

In this tutorial create 3 files
1. guestbook.php
2. addguestbook.php
3. viewguestbook.php

Step
1. Create table name “guestbook” in database “test”.
2. Create file guestbook.php.
3. Create file addguestbook. php.
4. Create file viewguestbook.php

1. Set up database

CREATE TABLE `guestbook` (
`id` int(4) NOT NULL auto_increment,
`name` varchar(65) NOT NULL default '',
`email` varchar(65) NOT NULL default '',
`comment` longtext NOT NULL,
`datetime` varchar(65) NOT NULL default '',
PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

2. Create file guestbook.php

CODE

<table width="400" border="0" align="center" cellpadding="3"  cellspacing="0">
<tr>
<td><strong>Test Sign Guestbook </strong></td>
</tr>
</table>
<table width="400" border="0" align="center" cellpadding="0"  cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form id="form1" name="form1" method="post"  action="addguestbook.php">
<td>
<table width="400" border="0" cellpadding="3" cellspacing="1"  bgcolor="#FFFFFF">
<tr>
<td width="117">Name</td>
<td width="14">:</td>
<td width="357"><input name="name"  type="text" id="name" size="40" /></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input name="email" type="text"  id="email" size="40" /></td>
</tr>
<tr>
<td valign="top">Comment</td>
<td valign="top">:</td>
<td><textarea name="comment" cols="40"  rows="3" id="comment"></textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit"  value="Submit" /> <input type="reset" name="Submit2" value="Reset"  /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<table width="400" border="0" align="center" cellpadding="3"  cellspacing="0">
<tr>
<td><strong><a href="viewguestbook.php">View  Guestbook</a> </strong></td>
</tr>
</table>

3. create file addguestbook.php
CODE

<?php
$host="localhost";  // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="guestbook"; // Table name

// Connect to server and select database.
mysql_connect("$host",  "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");

$datetime=date("y-m-d  h:i:s"); //date time

$sql="INSERT INTO  $tbl_name(name, email, comment, datetime)VALUES('$name', '$email',  '$comment', '$datetime')";
$result=mysql_query($sql);

//check if query successful
if($result){
echo "Successful";
echo "<BR>";
echo "<a  href='viewguestbook.php'>View guestbook</a>"; // link to view guestbook page
}

else {
echo "ERROR";
}

mysql_close();
?>

4. Create file viewguestbook.php

 

CODE

<table width="400" border="0" align="center" cellpadding="3"  cellspacing="0">
<tr>
<td><strong>View Guestbook | <a  href="guestbook.php">Sign Guestbook</a>  </strong></td>
</tr>
</table>
<br>

<?php

$host="localhost"; // Host name
$username="";  // Mysql username
$password=""; // Mysql password
$db_name="test";  // Database name
$tbl_name="guestbook";  // Table name

// Connect to server and select database.
mysql_connect("$host",  "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT  * FROM $tbl_name";
$result=mysql_query($sql);

while($rows=mysql_fetch_array($result)){
?>
<table width="400"  border="0" align="center" cellpadding="0" cellspacing="1"  bgcolor="#CCCCCC">
<tr>
<td><table  width="400" border="0" cellpadding="3" cellspacing="1"  bgcolor="#FFFFFF">
<tr>
<td>ID</td>
<td>:</td>
<td><?  echo $rows['id']; ?></td>
</tr>
<tr>
<td  width="117">Name</td>
<td  width="14">:</td>
<td width="357"><? echo $rows['name']; ?></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><?  echo $rows['email']; ?></td>
</tr>
<tr>
<td  valign="top">Comment</td>
<td  valign="top">:</td>
<td><?  echo $rows['comment']; ?></td>
</tr>
<tr>
<td  valign="top">Date/Time </td>
<td  valign="top">:</td>
<td><?  echo $rows['datetime']; ?></td>
</tr>
</table></td>
</tr>
</table>
<BR>
<?
}
mysql_close(); //close database
?>
Advertisements
Comments
  1. perchidloXZY says:

    дед лох
    на суплекс нах!

  2. sluddedly says:

    This amazing website saved me $487 for the year! They’re an online broker and they are kind, caring, and take wonderful care of the clientele. Most importantly for me, they answer customer emails and answer telephone calls with humans.

  3. one can argue that it can go both ways

  4. Tattoo Fonts says:

    TATTOO FONTS- suggestions? help me pick 🙂 http://www.tattoofonts.tk

  5. How great is that! I’m just like a dubmbest blond ever on MoodRaisers 🙂

  6. Елена says:

    Брачное агенство «Elmi» – это огромная база данных по всем городам Севера Израиля, персональный подход, огромный опыт и внимание. Мы предложим ту форму обслуживания, которая подходит именно Вам. Для нас важно, чтобы Вы чувствовали себя уверенно и комфортно. Мы поможем Вам найти своё семейное счастье

    Мы работаем в Хайфе, Краёт, Маалот Таршихе и других городах севера страны.

  7. John says:

    Hello thanks for code 🙂 I get to my site 🙂

  8. pissuttWede says:

    Hello all! I like this forum, i organize tons interesting people on this forum.!!!

    Pronounced Community, regard all!

  9. You made some respectable points there. I appeared on the internet for the issue and found most people will go together with with your website.
    This is the right blog for anybody who desires to find out about this topic. You understand so much its almost hard to argue with you (not that I truly would want…HaHa). You undoubtedly put a brand new spin on a subject thats been written about for years. Great stuff, just great!
    Your place is valueble for me. Thanks!…
    Cкрынник

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s