How to make your own guestbook using PHP/MySQL Requirements: - PHP 4+ - MySQL 3+ To start with we need to make a table for storing the guestbook entries, heres how you table structure should be: Code: CREATE TABLE `guestbook` ( `entry_id` INTEGER(6) UNSIGNED NOT NULL AUTO_INCREMENT, `fname` VARCHAR(45) NOT NULL, `location` VARCHAR(45) NOT NULL, `email` VARCHAR(60) NOT NULL, `dt` DATETIME NOT NULL, `comments` TEXT NOT NULL, PRIMARY KEY(`entry_id`) ) TYPE = MYISAM; Next we'll make two PHP scripts namely gbook.php & showgbook.php, one for collecting the entries and the other for showing the guestbook entries. File gbook.php : This file will have 2 sections one which collects the form data and the other which shows the form if no data was posted. This is how we will do it : PHP: if(isset($_POST['type'])) { //insert into database } else { //show the form } "type" is a hidden field in the form, this code block will be contained in the body of the HTML. Lets look at the if block first, heres what all it needs to do : - Collect the form data into some variables - Connect to the MySQL server - Build the SQL query - Execute the query - Show the user a message Here goes the code : PHP: //collect the posted data $name=addslashes($_POST['fname']); //addslashes escapes the single quotes $location=addslashes($_POST['location']); $email=trim($_POST['email']); //trim removes the leading and trailing whitespaces $comments=addslashes(nl2br($_POST['comments'])); //nl2br converts the newline characters to <BR/> //connect to the database $link=@mysql_connect("localhost","pradeep","password") or die("Could not connect : ".mysql_error()); //@ sign demystified :- the '@' tells the parser not to show any warnings //select the database to use @mysql_select_db("db",$link) or die("Could not select DB : ".mysql_error()); //build the query string $sql="INSERT INTO guestbook(fname,location,email,dt,comments) VALUES('$name','$location','$email',NOW(),'$comments')"; //execute the query @mysql_query($sql,$link) or die("Could not execute : ".mysql_error()); //show the user some message echo "<h3>Your entry was successfully inserted into the database,<br/>Thank you.</h3>"; //close the mysql connection mysql_close($link); } Now coming to the else block, just put in your form's HTML code there. File showgbook.php This file will show the guestbook entries, heres what all it needs to do: - Connect to the MySQL server - Bulid SQL query - Execute the query and fetch the resultset - Loop through the resultset and display each record in a format Here the SQL query : PHP: $sql="SELECT fname,location,email,UNIX_TIMESTAMP(dt),comments FROM guestbook ORDER BY dt"; NOw to to loop through the resultset : PHP: while($row=mysql_fetch_array($result)) //loop and fecth a single row everytime { echo "<br><table style=\"border:#666666 1px solid;font-family:Georgia;font-size:14px\">" ;//print the table tag into the page echo "<tr>"; //we will display the name ans date of signing in the first row of the table echo "<td bgcolor=\"#666666\"><font color=white>Signed by ".$row[0]." on ".date("l dS of F Y h:i:s A",$row[3])."</font></td></tr>";<br> echo "<tr><td>Location : $row[1]</td></tr>"; echo "<tr><td>Email : $row[2]</td></tr>"; echo "<tr><td>Comments :<br> $row[4]</td></tr>"; echo "</table><br>"; } Hope you enjoy making your own guestbook. Below you will find the complete code. GBOOK.PHP Code: <html> <head> <title>Guestbook</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <? /* CREATE TABLE `guestbook` ( `entry_id` INTEGER(6) UNSIGNED NOT NULL AUTO_INCREMENT, `fname` VARCHAR(45) NOT NULL, `location` VARCHAR(45) NOT NULL, `email` VARCHAR(60) NOT NULL, `dt` DATETIME NOT NULL, `comments` TEXT NOT NULL, PRIMARY KEY(`entry_id`) ) TYPE = MYISAM; */ if(isset($_POST['type']))//check whether the form was submitted { //collect the posted data $name=addslashes($_POST['fname']); //addslashes escapes the single quotes $location=addslashes($_POST['location']); $email=trim($_POST['email']); //trim removes the leading and trailing whitespaces $comments=addslashes(nl2br($_POST['comments'])); //nl2br converts the newline characters to <BR/> //connect to the database $link=@mysql_connect("localhost","pradeep","password") or die("Could not connect : ".mysql_error()); //@ sign demystified :- the '@' tells the parser not to show any warnings //select the database to use @mysql_select_db("db",$link) or die("Could not select DB : ".mysql_error()); //build the query string $sql="INSERT INTO guestbook(fname,location,email,dt,comments) VALUES('$name','$location','$email',NOW(),'$comments')"; //execute the query @mysql_query($sql,$link) or die("Could not execute : ".mysql_error()); //show the user some message echo "<h3>Your entry was successfully inserted into the database,<br/>Thank you.</h3>"; //close the mysql connection mysql_close($link); } else //if there was no form data simply show the form { ?> <form name="form1" method="post" action="gbook.php"> <table width="60%" height="36%" border="0" cellpadding="1" cellspacing="2" style="border:#666666 1px solid;font-family:Georgia;font-size:14px"> <tr> <td height="9%" colspan="2" bgcolor="#666666"><font color="#FFFFFF" face="Georgia, Times New Roman, Times, serif"><strong>Fill in my guestbook</strong></font></td> </tr> <tr> <td width="10%" height="11%">Name</td> <td><input name="fname" type="text" id="fname"></td> </tr> <tr> <td height="11%">Location</td> <td><input name="location" type="text" id="location"></td> </tr> <tr> <td height="11%">Email</td> <td><input name="email" type="text" id="email"></td> </tr> <tr> <td height="45%">Comments</td> <td><textarea name="comments" rows="5" id="comments"></textarea></td> </tr> <tr> <td height="13%"> <input name="type" type="hidden" id="type" value="goobk"></td> <td><input type="submit" name="Submit" value="Sign"></td> </tr> </table> </form> <? } //end of the if-else ?> </body> </html> SHOWGBOOK.PHP Code: <html> <head> <title>View GUestbook</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body>] <h2>View The Guestbook</h2> <?php //connect to the MySQL server $link=mysql_connect("localhost","pradeep","password") or die("Could not connect : ".mysql_error()); //select the database mysql_select_db("db",$link) or die("Could not select DB : ".mysql_error()); //build the query $sql"SELECT fname,location,email,UNIX_TIMESTAMP(dt),comments FROM guestbook ORDER BY dt"; //execute the query $result=mysql_query($sql,$link); //now we need to display the data in tabular for, so we will loop thru the resultset while($row=mysql_fetch_array($result)) //loop and fecth a single row everytime { echo "<br><table style=\"border:#666666 1px solid;font-family:Georgia;font-size:14px\">" ;//print the table tag into the page echo "<tr>"; //we will display the name ans date of signing in the first row of the table echo "<td bgcolor=\"#666666\"><font color=white>Signed by ".$row[0]." on ".date("l dS of F Y h:i:s A",$row[3])."</font></td></tr>";<br> echo "<tr><td>Location : $row[1]</td></tr>"; echo "<tr><td>Email : $row[2]</td></tr>"; echo "<tr><td>Comments :<br> $row[4]</td></tr>"; echo "</table><br>"; } mysql_free_result($result); mysql_close($link); ?> </body> </html>
This is the code for add_post.php <? if($_POST['nume_utilizator'] == "" || $_POST['adresa_email'] == "" || $_POST['comentariu'] == "") { print "Trebuie sa completezi toate campurile!"; exit; } mysql_connect("localhost","user","password"); mysql_select_db("your_database"); $nume = strip_tags($_POST['nume_utilizator']); $mail = strip_tags($_POST['adresa_email']); $comentariu = strip_tags($_POST['comentariu']); $sql = "insert into comentarii(nume_utilizator,adresa_email,comentariu) values('".$nume."','".$mail."','".$comentariu."')"; mysql_query($sql); header("location: guestbook.php"); ?> ------------------ albertjames