Tutorial: guest book
-
- Posts: 348
- Joined: Thu Jan 12, 2006 8:24 pm
- Contact:
Tutorial: guest book
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
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
-
- Posts: 348
- Joined: Thu Jan 12, 2006 8:24 pm
- Contact:
gb
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
Name
location
date of birth
website
message
rate my site [compo box] 1, 2,3,4,5,6,7,8,9,10
thanks
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
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

-
- Posts: 348
- Joined: Thu Jan 12, 2006 8:24 pm
- Contact:
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

Then, we have to create fields for data. Well need such fields: id, name, location, date of birth (lets 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 Im suggesting you to use ID.
So now I will show what types of fields should be

After pressing save you should se a message
Table gbook has been created. SQL-query:
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 Ill post here only a source, use notepad to save it into file save.php.
Index.php
Mysql.php
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

Then, we have to create fields for data. Well need such fields: id, name, location, date of birth (lets 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 Im suggesting you to use ID.
So now I will show what types of fields should be
So we will have such structure in mysql. Create all fields names in lowercase letters.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

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` )
);
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

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>
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>
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

Lixas

-
- Posts: 348
- Joined: Thu Jan 12, 2006 8:24 pm
- Contact: