<html>
<head>
<title>Guestbook</title>
<meta http-equiv="Content-Type" content="text/html; charset=cp1251">
</head>
<body>
<form method="post" accept-charset="cp1251">
Your name:<br>
<input type="text" size=40 name="name">
<br>
Your message:<br>
<textarea name="post" rows=10 cols=30></textarea>
<br>
<input type="submit" value="Post a message">
<input type="hidden" name="input" value='1'>
<br>
</form>
<?PHP
/*Very simple and without any special design guest book
with only possibility to post messages and names.
It doesn't support any cookies or sessions - that means
that there is no special possibility to moderate it.
There are some moderate options to Delete messages and
to Reset the whole Database, but it is available for
every visitor, so i strictly advise your to comment it!
The best way to moderate guest book is to use phpmyadmin*/
// Some otions of MySQL connection
$host="localhost";
$login="root";
$pass="";
$db_name="db_book";
$table="data";
//Connecting to MySQL and customizing encoding detection
mysql_query ("set collation_connection='cp1251_general_ci'"); //Reseting guestbook if special link clicked (Comment it!!!)
$action=isset($_GET['action'])?
$_GET['action']:''; if ($action=="NULL")
{
$query="DROP DATABASE IF EXISTS `$db_name`;";
}
//Creating and selecting the database if it doesn't exist
$query="CREATE DATABASE IF NOT EXISTS `$db_name`;";
//Creating the table if it doesn't exist
$query="CREATE TABLE IF NOT EXISTS `$table` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 20 ) CHARACTER SET cp1251 COLLATE cp1251_bin NOT NULL ,
`post` VARCHAR( 500 ) CHARACTER SET cp1251 COLLATE cp1251_bin NOT NULL
);";
//Deleting message which is chosen (doesn't decrease id in mysql)
$id=isset($_GET['id'])?
$_GET['id']:''; if ($id)
{
$query="DELETE FROM `$table` WHERE `id`=$id";
}
//Reading the form and checking for a correct post
$name=isset($_POST['name'])?
$_POST['name']:''; $post=isset($_POST['post'])?
$_POST['post']:''; $test=1;
if (isset($_POST['input'])) {
{
echo ("You haven't specified your name!<br><br>");
$test=0;
}
elseif ((strlen($name)>20
)&&($test)) {
echo ("Your name is too long!<br><br>");
$test=0;
}
elseif ((strlen($post)<1
)&&($test)) {
echo ("You haven't written the message!<br><br>");
$test=0;
}
elseif ((strlen($post)>500
)&&($test)) {
echo ("Your message is too long!<br><br>");
$test=0;
}
elseif ($test)
{
//Inserting data inside the table if post is a correct one
$query="INSERT INTO `$table` (name, post) VALUES ('$name','$post')";
}
}
//Reading and showing all storaged posts
$query="SELECT id,name,post FROM `$table` ORDER BY id DESC";
$counter=0;
{
$counter++; //Counting number of posts
$current="id";
foreach ($name as $index => $value)
{
if ($index=="id") //Showing post id
{
echo "<b>Post ID: $value ";
$id=$value;
}
elseif ($index=="name") //Showing post author
{
echo "Post author: $value</b>";
echo " <a href='./index.php?id=$id'>[Delete post]</a><br>"; //And showing 'Delete' link (Better to comment it!)
}
else
{
//Parsing some bb codes into html
//Showing our message
echo "$value<br><hr>";
}
}
}
//Showing number of posts
if (!$counter) {
echo "<i>The guestbook is empty!</i>";
}
else
{
echo "The total number of posts: <b>$counter</b>";
}
//'Reset guestbook' link (Drops database and resets id count) Better to comment it until it's needed!!!
echo "<br><br><a href='./index.php?action=NULL'><b><i>Reset guestbook (Carefully! Drops database!)</b></i></a>";
?>
</body>
</html>