simple guestbook in php

php-sniplets Add comments

Simple guestbook

Lets visitors to your web site to read your guestbook entries and send a message of their own. Really simple
setup, just needs you to modify 4 settings. Utilizes MySQL to store the entries.

Code starts here


* Create the table in your MySQL database:
* CREATE TABLE guest (
*   id int(10) NOT NULL auto_increment,
*   name varchar(50) NOT NULL,
*   message varchar(255) NOT NULL,
*   date timestamp(14) NOT NULL,
*   PRIMARY KEY (id)
* )
* Change the database settings to your personal settings
* The script is now ready to go !!


// Change here to your personal database settings

$host = “yourlocalhost”;
$user = “yourusername”;
$pass = “yourpassword”;
$db = “yourdatabase”;

mysql_connect($host, $user, $pass) OR die (“Could not connect to the server.”);

mysql_select_db($db) OR die(“Could not connect to the database.”);

$name = stripslashes($_POST['txtYName']);

$message = stripslashes($_POST['txtYMessage']);

if (!isset($_POST['txtName'])) {

$query = “SELECT id, name, message, DATE_FORMAT(date, ‘%D %M, %Y @ %H:%i’) as newdate
FROM guests ORDER BY id DESC”;

$result = mysql_query($query);

while ($row = mysql_fetch_object($result)) {


<p><strong><?php echo $row->message; ?></strong>

<br />Posted of <?php echo $row->name; ?> on <?php echo $row->newdate; ?></p>



<p>Post your message</p>

<form method=”post” action=”<?php echo $_SERVER['REQUEST_URI']; ?>”>

<p><label for=”txtYName”>Your Name Here:</label><br />

<input type=”text” title=”Put your name here” name=”txtName” /></p>

<p><label for=”txtYMessage”>Put Your messageHere:</label><br />

<textarea title=”Enter your message” name=”txtMessage”></textarea></p>

<p><label title=”Send your message”>

<input type=”submit” value=”Send” /></label></p>




else {

// Adds the new entry to the database

$query = “INSERT INTO guest SET message=’$messag’, name=’$names’, date=NOW()”;

$result = mysql_query($query) or die(mysql_error());

// Take back to the entries


header (“Location: $ref”);


Leave a Reply

You must be logged in to post a comment.

Impressum Entries RSS Log in