PHP Guestbook Tutorial

Creator:
Michael4

This will learn you how to make a guestbook in PHP Here is the SQL code

CREATE TABLE `guestbook` ( `id` INT NOT NULL AUTO_INCREMENT, `name` TEXT NOT NULL, `message` TEXT NOT NULL PRIMARY KEY(`id`) );

Here is the code for guestbook.php

<?php mysql_connect('server','username','password'); mysql_select_db('database'); $queryy = "SELECT * FROM `guestbook` WHERE `id`!='0' ORDER BY `id` DESC"; $fft = mysql_query($queryy); while($ffft = mysql_fetch_array($fft)){ echo "Name: ".$ffft['name']."<br/><br/>Message: ".$ffft['message']."<br/><br/>"; } ?>

I will explain the code mysql_connect('server','username','password'); mysql_select_db('database'); This will connect to the database, you need to change server, username and password and database.

$queryy = "SELECT * FROM `guestbook` WHERE `id`!='0' ORDER BY `id` DESC"; $fft = mysql_query($queryy);

This will select everything from the table guestbook and execute the query.

while($ffft = mysql_fetch_array($fft)){ echo "Name: ".$ffft['name']."<br/><br/>Message: ".$ffft['message']."<br/><br/>";

This is a while loop and this will print out the guestbook messages. Here is the code for addmessage.php

<?php mysql_connect('server','username','password'); mysql_select_db('database'); if(!isset($_POST['submit'])){ echo "<form action='' method='post'> Your name: <input type='text' name='yourname'><br/> Your message: <textarea name='yourmessage'></textarea><br/> <input type='submit' name='submit' value='Submit Message'></form>"; }else{ $rrrt = "INSERT INTO `guestbook` (`id`,`name`,`message`) VALUES ('','".$_POST["yourname"]."','".$_POST["yourmessage"]."');"; mysql_query($rrrt); echo "You have successfully submitted your message<br/>Thank you for submitting your message"; } ?>

I will explain this code

mysql_connect('server','username','password'); mysql_select_db('database');

This will connect to the database, you need to change the server, username and password and database.

if(!isset($_POST['submit'])){ echo "<form action='' method='post'> Your name: <input type='text' name='yourname'><br/> Your message: <textarea name='yourmessage'></textarea><br/> <input type='submit' name='submit' value='Submit Message'></form>";

This checks to see if the Submit Message button isn't set, it will print out the form.

}else{ $rrrt = "INSERT INTO `guestbook` (`id`,`name`,`message`) VALUES ('','".$_POST["yourname"]."','".$_POST["yourmessage"]."');"; mysql_query($rrrt); echo "You have successfully submitted your message<br/>Thank you for submitting your message";

If the Submit Message is set, it will insert the data in the database and execute the query and print out a message saying you have successfully submitted your message thank you for submitting your message.

Thank you for reading PHP Guestbook tutorial Thank you Michael

Description:
This tutorial will learn you how to make a guestbook in PHP using a database

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options