Tuesday, 20 March 2012

PHP Guest Book project code

Create a table guestbook in MYSQL
=========================
mysql> create databse guestbook;               
mysql> use guestbook;

CREATE TABLE IF NOT EXISTS `guestbook` (
  `name` varchar(40) DEFAULT NULL,
  `location` varchar(40) DEFAULT NULL,
  `email` varchar(40) DEFAULT NULL,
  `url` varchar(80) DEFAULT NULL,
  `comments` text
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
 
 INSERT COMMAND
================= 
INSERT INTO `guestbook` (`name`, `location`, `email`, `url`, `comments`) VALUES
('sateesh', 'seet', 'bagadhi.sateesh@gmail.com', 'http://bagadhicomputersciencetutorials.blogspot.com', 'hi');


====================
Create the following php files
1)dbconnect.php
2) create_entry.php
3)sign.php
4)view.php

code for the above files
------------------------------
 
1)dbconnect.php
 
<html>
<body>
<?php
mysql_connect("localhost", "sateesh","bagadhi") or
 die ("Could not connect to database");
mysql_select_db("guestbook") or
 die ("Could not select database");
?>
</body>
</html>




 
2)create_entry.php
 
<html>
<body>
<?php 
include("dbconnect.php");
$submit = "Sign!";
$name=$_POST['name'];
$location=$_POST['location'];
$email=$_POST['email'];
$url=$_POST['URL'];
$comments=$_POST['comments'];
if ($submit == "Sign!")
{
$query = "insert into guestbook
(name,location,email,url,comments) values
('$name','$location','$email','$url','$comments')";
mysql_query($query) or die (mysql_error());
?>
<h2>Thanks!</h2>
<h2> <a href="view.php">View My Guest Book!</a></h2>
<?php
}
else
{
include("sign.php");
}
?>
</body>
</html> 
 
3)sign.php
 
<html>
<body>
<h2>Sign my Guest Book!</h2>
<form method=post action="create_entry.php">
<b>Name  :<t></t></b>
<input type=text size=40 name=name>
<br>
<b>Location :</b>
<input type=text size=40 name=location>
<br>
<b>Email :<t></t></b>
<input type=text size=40 name=email>
<br>
<B>Home page URL:</B>
<INPUT TYPE=TEXT SIZE=40 NAME=URL>
<BR>
<b>comments :<t></t></b>
<textarea name=comments cols=40 rows=10 wrap=virtual></textarea>
<br>
<input type=submit name=submit value="Sign!">
<input type=reset name=reset value="Start Over">
</form>
</body>
</html> 
4)view.php
 
<html>
<body>
<?php include("dbconnect.php"); ?>
<h2>View My Guest Book!</h2>
<?php
$result = mysql_query ("select * from guestbook") or
 die (mysql_error());
while ($row = mysql_fetch_array($result))
{
echo "<b>Name:</b>";
echo $row["name"];
echo "<br>\n";
echo "<b>Location:</b>";
echo $row["location"];
echo "<br>\n";
echo "<b>Email:</b>";
echo $row["email"];
echo "<br>\n"; 
echo "<b>URL:</b>";
echo $row["url"];
echo "<br>\n"; 
echo "<b>Comments:</b>";
echo $row["comments"];
echo "<br>\n";
echo "<br>\n";
echo "<br>\n";
}
mysql_free_result($result);
?>
<h2> <a href="sign.php">Sign My Guest Book!</a></h2>
<body>
</html>

 Save all files and run in localhost
sign.php 





No comments:

Post a Comment