Hypertext
15-02-2008, 05:33 AM
OK, today I tried the whole day to create a guestbook, so i'm going to be sharing it with you.
Through I will refer to lines as L1, L2, L3 etc.
Please read the comments to understand what it is all about, don't just c+p! Trust me it's alot more satisfying to read the comments..
Without further ado here we go
Assuming you have MySQL already installed we're going to need to create a database, the sql code for this is here:
CREATE TABLE `guestbook` (
`id` int(4) NOT NULL auto_increment,
`name` varchar(65) NOT NULL default '',
`email` varchar(65) NOT NULL default '',
`message` longtext NOT NULL,
`datetime` varchar(65) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;Lo down: L1: creating the table with a name 'guestbook'. L2: creating the id column. L3: creating the name column. L4: creating the email column. L5: creating the comment column. L6: creating the datetime (of creation column).
Now, create a new file named guestbook.php.
<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><strong>myGUESTBOOK</strong></td>
</tr>
</table>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form id="gb" name="gb" method="post" action="addgb.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">Message</td>
<td valign="top">:</td>
<td><textarea name="message" cols="40" rows="3" id="message"></textarea></td>
</tr>
<tr>
<td> </td>
<td> </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>Lo down: I assume you have sufficient knowledge to understand this HTML. So I won't go over it, other than that it's a form, and has several inputs and has an action of 'addgb.php'
Now create a file 'addgb.php'.
<?php
$host="myhost"; // Host
$username="myuser"; // username
$password="mypassword"; // password
$db_name="mydatabasename"; // Db name
$tbl_name="guestbook"; // Table name
// now we connect to the database
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB"); //selecting the db
$datetime=date("y-m-d h:i:s"); //creating the current date and time to use on the message
$sql="INSERT INTO $tbl_name(name, email, message, datetime)VALUES('$name', '$email', '$message', '$datetime')"; //choosing the fields to insert, and inserting the data using the variables
$result=mysql_query($sql); //query the db
//check if query successful
if($result){ //if it works
echo "Successful"; //great
echo "<BR>";
echo "<a href='viewguestbook.php'>View guestbook</a>"; // link to see the db
}
else {
echo "Somethings gone wrong!"; // oh noes we gots an error...
}
mysql_close(); //closes connection
?>
Lo Down: Within the comments ;)
Nowwww... the fun part viewing and formatting the viewguestbook.php ok so create viewguestbook.php
I am one tired and two uncreative, so for now we will use a rather... basic look :P, you can snazz it up yaselves!
<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="myhost"; // Host
$username="myusername"; // username
$password="mypassword"; // password
$db_name="mydb"; // Db name
$tbl_name="guestbook"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die(mysql_error());
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name"; // query
$result=mysql_query($sql); //querying
while($rows=mysql_fetch_array($result)){ // interpreting the data
?>
<html>
<head>
<title> myGUESTBOOK! </title>
</head>
<body bgcolor="#999999">
<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']; // echoing the name?></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><? echo $rows['email']; // echoing the email ?></td>
</tr>
<tr>
<td valign="top">Comment</td>
<td valign="top">:</td>
<td><? echo $rows['message']; // echoing the message ?></td>
</tr>
<tr>
<td valign="top">Date/Time </td>
<td valign="top">:</td>
<td><? echo $rows['datetime']; // echoing the date and time ?></td>
</tr>
</table></td>
</tr>
</table>
<BR>
<?
} // finishing the statement
mysql_close(); //ending any remaining connections
<BR>
<BR>
<BR>
Coded by <strong>Charlie</strong> known as <strong>Reconix</strong> on <a href="http://habboxforum.com/showthread.php?t=455506"><strong>HabboxForum</strong></a> from <a href="http://www.codeetech.com/"><strong>CodeeTech</strong></a>
</body>
</html>
Thanks for bearing with me throughout this boring tut ;)
Well, I feel satisfactory, do you?
You don't have to keep the bottom little credit, but if you can it's always nice
Through I will refer to lines as L1, L2, L3 etc.
Please read the comments to understand what it is all about, don't just c+p! Trust me it's alot more satisfying to read the comments..
Without further ado here we go
Assuming you have MySQL already installed we're going to need to create a database, the sql code for this is here:
CREATE TABLE `guestbook` (
`id` int(4) NOT NULL auto_increment,
`name` varchar(65) NOT NULL default '',
`email` varchar(65) NOT NULL default '',
`message` longtext NOT NULL,
`datetime` varchar(65) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;Lo down: L1: creating the table with a name 'guestbook'. L2: creating the id column. L3: creating the name column. L4: creating the email column. L5: creating the comment column. L6: creating the datetime (of creation column).
Now, create a new file named guestbook.php.
<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><strong>myGUESTBOOK</strong></td>
</tr>
</table>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form id="gb" name="gb" method="post" action="addgb.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">Message</td>
<td valign="top">:</td>
<td><textarea name="message" cols="40" rows="3" id="message"></textarea></td>
</tr>
<tr>
<td> </td>
<td> </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>Lo down: I assume you have sufficient knowledge to understand this HTML. So I won't go over it, other than that it's a form, and has several inputs and has an action of 'addgb.php'
Now create a file 'addgb.php'.
<?php
$host="myhost"; // Host
$username="myuser"; // username
$password="mypassword"; // password
$db_name="mydatabasename"; // Db name
$tbl_name="guestbook"; // Table name
// now we connect to the database
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB"); //selecting the db
$datetime=date("y-m-d h:i:s"); //creating the current date and time to use on the message
$sql="INSERT INTO $tbl_name(name, email, message, datetime)VALUES('$name', '$email', '$message', '$datetime')"; //choosing the fields to insert, and inserting the data using the variables
$result=mysql_query($sql); //query the db
//check if query successful
if($result){ //if it works
echo "Successful"; //great
echo "<BR>";
echo "<a href='viewguestbook.php'>View guestbook</a>"; // link to see the db
}
else {
echo "Somethings gone wrong!"; // oh noes we gots an error...
}
mysql_close(); //closes connection
?>
Lo Down: Within the comments ;)
Nowwww... the fun part viewing and formatting the viewguestbook.php ok so create viewguestbook.php
I am one tired and two uncreative, so for now we will use a rather... basic look :P, you can snazz it up yaselves!
<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="myhost"; // Host
$username="myusername"; // username
$password="mypassword"; // password
$db_name="mydb"; // Db name
$tbl_name="guestbook"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die(mysql_error());
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name"; // query
$result=mysql_query($sql); //querying
while($rows=mysql_fetch_array($result)){ // interpreting the data
?>
<html>
<head>
<title> myGUESTBOOK! </title>
</head>
<body bgcolor="#999999">
<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']; // echoing the name?></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><? echo $rows['email']; // echoing the email ?></td>
</tr>
<tr>
<td valign="top">Comment</td>
<td valign="top">:</td>
<td><? echo $rows['message']; // echoing the message ?></td>
</tr>
<tr>
<td valign="top">Date/Time </td>
<td valign="top">:</td>
<td><? echo $rows['datetime']; // echoing the date and time ?></td>
</tr>
</table></td>
</tr>
</table>
<BR>
<?
} // finishing the statement
mysql_close(); //ending any remaining connections
<BR>
<BR>
<BR>
Coded by <strong>Charlie</strong> known as <strong>Reconix</strong> on <a href="http://habboxforum.com/showthread.php?t=455506"><strong>HabboxForum</strong></a> from <a href="http://www.codeetech.com/"><strong>CodeeTech</strong></a>
</body>
</html>
Thanks for bearing with me throughout this boring tut ;)
Well, I feel satisfactory, do you?
You don't have to keep the bottom little credit, but if you can it's always nice