Learn how to Make Money Online doing freelancing, Affiliate Marketing, Blogging and many more ...
Go4Expert
Go4Expert RSS Feed

Go Back   Programming and SEO Forum >  Go4Expert > Queries and Discussion > Web Development > PHP

Reply  Copy HTML to Clipboard  Copy BBCode to Clipboard  | More
 
Bookmarks Thread Tools Search this Thread Display Modes
Old 04-10-2005, 11:12 AM   #1
Team Leader
 
pradeep's Avatar
 
Join Date: Apr 2005
Location: Kolkata, India
Posts: 1,470
Thanks: 0
Thanked 35 Times in 30 Posts
Rep Power: 7
pradeep will become famous soon enough
Send a message via Yahoo to pradeep
Lightbulb

GUestbook in PHP


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 Code:
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 Code:
 //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 Code:
$sql="SELECT fname,location,email,UNIX_TIMESTAMP(dt),comments FROM guestbook ORDER BY dt"
NOw to to loop through the resultset :

PHP Code:
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>
pradeep is offline   Reply With Quote
Old 09-15-2008, 08:01 AM   #2
Newbie Member
 
Join Date: Sep 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
albertjames is on a distinguished road

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

Last edited by shabbir; 09-15-2008 at 09:34 AM. Reason: Confine links to signatures only.
albertjames is offline   Reply With Quote
Reply  Copy HTML to Clipboard  Copy BBCode to Clipboard  | More


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Bookmarks

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads / Articles
Thread Thread Starter Forum Replies Last Post
PHP On-The-Fly! Kings PHP 5 05-01-2008 11:28 AM
Track your visitors, using PHP Kings PHP 8 12-11-2006 01:53 AM
Mastering Regular Expressions in PHP Kings PHP 5 03-29-2005 09:06 AM
PHP and Cookies; a good mix! Kings PHP 1 12-07-2004 08:53 AM
Begining with PHP shabbir PHP 0 07-15-2004 03:22 PM

 

All times are GMT +5.5. The time now is 05:23 AM.