Page 1 of 1

Tutorial: guest book

Posted: Mon Jan 16, 2006 6:19 pm
by jasondsouza
does anyone know how to create a guest book using php

please help i am keen to create one

i will also accept anyone telling me a good site to learn about it

Moderated by Lixas

Posted: Mon Jan 16, 2006 9:06 pm
by Lixas
i could help you with this. Just post here what u whant to do exactly, and i will help u to make guest book. My experience in php is enought to create sutch scripts :)

gb

Posted: Tue Jan 17, 2006 4:44 pm
by jasondsouza
I want to create a guest book with the following fields using php

Name
location
date of birth
email
website
message
rate my site [compo box] 1, 2,3,4,5,6,7,8,9,10

thanks

Posted: Tue Jan 17, 2006 5:05 pm
by Lixas
i would like you to ask one question. wher you will save all your data
i could help you with mysql and with flatfiles (no data server nedded, only flat file on web server). I could suggest you mysql because it is easier to manipulate data, so now i'm waiting for your answer, and then i'll begin to write step by step tutorial :)

NOTE: Smokyhosts support mysql

Posted: Tue Jan 17, 2006 5:14 pm
by jasondsouza
please help me to embed that file in html first of all

i would like to go with my sql


thanks

Posted: Tue Jan 17, 2006 7:55 pm
by Lixas
First of all you have to create new mysql database (or you can use existing one). You can do this via phpmyadmin (search in CPanel, now I do not have account in smokyhosts, so I can not tell you exact path, but I remember that this is not difficult) Then create a new table. Go to database and search for such image as u see below
Image

Then, we have to create fields for data. We’ll need such fields: id, name, location, date of birth (let’s name this field “birth_day”), email, website, message, rating (1, 2,3,4,5,6,7,8,9,10 as available option). I will explain only what for is id. ID is a record number, I always use it in all my tables, so I’m suggesting you to use ID.
So now I will show what types of fields should be
ID – MEDIUMINT (5) extra-> Auto_increment and primary (radio button with key)
Name – VARCHAR (150) NULL default- No Name
Location – VARCHAR (250) NULL
Birth_Day – DATE(10) NULL
Email- VARCHAR(250) NULL
Website- VARCHAR(250) NULL
Message- TEXT NOT_NULL (do not write any numbers in “lenght”)
Rating- TINYINT(2) NULL
So we will have such structure in mysql. Create all fields names in lowercase letters.
Image
After pressing save you should se a message
Table gbook has been created. SQL-query:

Code: Select all

CREATE TABLE `gbook` (
`id` MEDIUMINT( 5 ) NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 150 ) DEFAULT 'No Name',
`location` VARCHAR( 250 ) ,
`birth_day` DATE,
`email` VARCHAR( 250 ) ,
`website` VARCHAR( 250 ) ,
`message` TEXT NOT NULL ,
`rating` TINYINT( 2 ) ,
PRIMARY KEY ( `id` ) 
);
NOTE: If you know how to use sql queries, u can use it for creating mysql table.

So now we have created mysql table for our guest book. The next step to we have to create html form for recording a new message :) I will create just a simple form without any super duper scripts and without good looking and I’ll post here only a source, use notepad to save it into file “save.php”.

Code: Select all

<?
if ($_POST["action"]=="submit")
{
	include("mysql.php"); // we include this file to connect to our database
	if (isset($_POST["name"])) $name=$_POST["name"]; //if there is any data in Name field, let's write it to variable $name
	if (isset($_POST["location"])) $location=$_POST["location"]; //the same here for location field and so on for others
	if (isset($_POST["birth_day"])) $b_day=$_POST["birth_day"]; 
	if (isset($_POST["email"])) $email=$_POST["email"]; 
	if (isset($_POST["website"])) $web=$_POST["website"];
	if (isset($_POST["message"])) $message=$_POST["message"]; 
	if (isset($_POST["rating"])) $rating=$_POST["rating"];
	mysql_query("INSERT INTO gbook (name, location, birth_day, email, website, message, rating) VALUES ('$name','$location','$b_day','$email','$web','$message', '$rating')") or die("Can not insert values. Error: ".mysql_error());

	$title="Thank You!";
	$done="1"; //we have recorded new record
}
else
{
	$title="New Comment";
	$done="0";
	
}

?>
<html>
<head>
<title><?echo $title?></title>
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
function close_win(msg)
	{
	opener.location.href='index.php';
	self.close();
	}
</script>
</head>
<?if ($done==0)
{?>
<body>
[align=center]
<form method="POST" action="save.php">
Name: <input type="text" name="name" size="20">

Location: <input type="text" name="location" size="20">

Day of birth: <input type="text" name="birth_day" size="20"> Format: <? echo date(Y.".".m.".".d);?>

E-mail: <input type="text" name="email" size="20">

WebSite: <input type="text" name="website" size="20">

Message: <textarea name="message" rows="12" cols="20" wrap="soft"></textarea>

Rating<select name="rating">
	<?
	#we will use php loop to create dropdown memu
	for($i=1; $i<11; $i++)
		{
		echo "<option value=\"$i\">$i</option>"; //to echo " to browser, u have to use slash before "
		}
	?>
</select>

<input type="hidden" name="action" value="submit">
<input type="submit" value="Go"><input type="reset" value="Clear all fields">
</p>
</form>[/align]
<?}
	else {?>
	<body onload="close_win()">
[align=center]Thank You! Your comment has been recorded! [align=center]
<?}?>

</body></html>
Index.php

Code: Select all

<?
include("mysql.php"); // we include this file to connect to our database
?>
<html>
<head>
<title>Guest Book</title>
</head>
 <SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
function newpop(url)
{
var windowprops = "width=450, height=450, location=no, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, top=50, left=200";
newwindow=window.open(url,'news_pop',windowprops);
}
</script>
<body>
[url=javascript:void(0)]<font size="5">Post</font>[/url]<hr width="80%">

<?
$rez=mysql_query("SELECT * FROM `gbook` order by id desc") or die(mysql_error());
$n=mysql_num_rows($rez);

#in this for loop, you can format how do u want to display records from database
#to get data, use $r["rowname"] 
for ($i=0; $i<$n; $i++)
{
$r=mysql_fetch_array($rez);
$name=$r["name"];
$email=$r["email"];
$location=$r["location"];
$birth_day=$r["birth_day"];
$website=$r["website"];
$rating=$r["rating"];
$message=$r["message"];
echo "$name:
 $message<hr>"; 
}
// and use all html formating tools to output data as you want, and dont foget abou quotes, if you want to oupot quotes, use slash
?>
</body>
</html>
Mysql.php

Code: Select all

<?
function mysqlc(){
	$server = 'localhost'; // server adress, mostly localhost
	$user = 'root'; //username
	$pass = 'some_kind_of_password'; //password
	$database = 'guestbook'; //database name
	
	mysql_connect($server, $user, $pass) or die("Can not connect to server. Error: ".mysql_error());
	mysql_select_db($database) or die("Can not select database. Error: ".mysql_error());}

	mysqlc()
?>

Finished, it tooked two hours to create this stuff, next time i will suggest google for sutch script :) good luck with learning. If you will have any problems, just pm me, i'll allways help you

Lixas

Posted: Wed Jan 18, 2006 5:56 pm
by jasondsouza
Thanks Lixas YOu were a great help

Posted: Tue Feb 14, 2006 2:07 pm
by vshost
Great thanks. I actually wrote myself a guestbook a while ago and a lot of people use it.

Posted: Mon Apr 17, 2006 3:04 am
by mohumben
As an alternative to MySQL, you can also use files as databases.
Basically, use the file handling functions to handle the files (fopen(), fwrite(), etc.) and serialize() to make data "storable".

Posted: Mon Jul 24, 2006 1:58 am
by jeremy90
thanks for the code Lixas, i might want to use that in the future