how to make simple guestbook with php

Here in this PHP tutorial, we will give you code sample for, how to make simple guestbook with php. we given code sample with database schema. we explained every step. Here’s an easy way to create guestbook with php.

how to make simple guestbook with php

First you have to create database for it. If your database has been created add this table to database.


 CREATE TABLE IF NOT EXISTS `posts` (

 `name` varchar(100) NOT NULL,

 `email` varchar(100) NOT NULL,

 `website` varchar(100) NOT NULL,

 `message` text NOT NULL,

 `ip` varchar(25) NOT NULL,

 `date` varchar(50) NOT NULL

 ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Step 1: Creating A Form

First we have to create a form. to do this use the following PHP code:


<code><code>echo "php?act=add' method='post'>"</code></code>

."Name:
<input type='text' name='name' size='30' />
"

."Email:
<input type='text' name='email' size='30' />
"

."Website:
<input type='text' name='website' size='30' />
"

."Message:
<textarea cols='30' rows='8' name='message'>
"

."<input type='submit' value='Post' />"

."</form>";

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Step 2: Add Info To Database

Now we have to add info to database. Do this using by this PHP code:

if($_GET['act'] == "add") </code></code>

<code><code>{ // If act is "add" like this file.php?act=add display the content</code></code>

$sqlCon = mysql_connect("localhost", "root", "bananaman"); // Connect to database

if($sqlCon == true){ // If connection has been made then..

$sqlSel = mysql_select_db("guestbook", $sqlCon); // Select a database

if($sqlSel == true)

<code><code>{ // If database has been successfully selected gather info and post it</code></code>

$name = addslashes(htmlspecialchars($_REQUEST['name'])); // Takes "name" field from the form

$email = addslashes(htmlspecialchars($_REQUEST['email'])); // Takes "email" field from the form

$website = addslashes(htmlspecialchars($_REQUEST['website'])); // Takes "website" field from the form

$message = addslashes(htmlspecialchars($_REQUEST['message'])); // Takes "message" field from the form

$ip = $_SERVER['REMOTE_ADDR']; // This takes user's IP

$time = time(); // Get UNIX timestamp

// This will create a SQL query which inserts all that information into database

$sql = "INSERT INTO posts (name, email, website, message, ip)

<code><code>VALUES ('".$name."', '".$email."', '".$website."', </code></code>

<code><code>'".$message."', '".$ip."', '".$time."')";</code></code>

// This will process SQL query and performs inserting info to database

$query = mysql_query($sql);

if($query == true) { // If everything was correct ..

echo "Your post has been successfully posted!"; // Display a success message

}else { // But if there was something wrong ..

echo "There was something wrong."; // Display a fail message

// As you can see I have commented

// mysql_error() function. It is because

// if you want to see what went wrong with

// your SQL query. Displaying it is not very

// good idea because it's like a candy to

// hackers. So if you have fixed the problem

// make sure to add comment "//" in front of it

//echo mysql_error();

}

mysql_close($sqlCon); // Close connection to database

}else{

exit; // If database selection wasn't a success close script

}

}else{

exit; // If connecting to database wasn't a success close script

}

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Step 3: Displaying Posts

Now posting part is done, now we're going to display our posts.

Use this PHP code:


<code><code>$sqlCon = mysql_connect("localhost", "root", "bananaman"); // Connect to database</code></code>

if($sqlCon == true) { // If connecting was successful ..

$sqlSel = mysql_select_db("guestbook", $sqlCon); // select your database

if($sqlSel == true) { // If database selecting was successful then take info from it

// Select all records from database

$sql = "SELECT * FROM posts";

// Process your selection

$query = mysql_query($sql);

// This creates a table which contains all info

// of your posts

while($info = mysql_fetch_array($query)) {

echo "<table width='300' border='1'>"

."<tr>"

."<td>Posted by: <a href='".$info['website']."'>".$info['name']."</a> (<a href='mailto:".$info['email']."'>Email</a>)</td>"

."".date("H:i j F Y", $info['date']).""

."</tr>"

."<tr>"

."<td colspan='2'>".$info['message']."</td>"

."</tr>"

."</table>"

."
";

}

mysql_close($sqlCon); // Close database connection

}else { // If database selection wasn't successful ..

exit; // exit form the script

}

}else { // If connection to database wasn't success ..

exit; // exit from your script

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

And you are done with your script. It should look like this:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Thank you!

how to send email using php – Using PHPmailer

Many PHP programmers facing this issue and hitting same wall. Here in article we shown, how to send email using php. we given code sample with phpmailer.

how to send email using php

how to send email using php - PHP tutorial
how to send email using php – PHP tutorial

PHP itself provides the mail() function to send emails. In this article I will show you the options of sending email using PHP language.

If you are using linux or windows hosting service for running the PHP application. PHP mail() function uses the sendmail mail server to send emails.
If sendmail mail server is configured and running on linux server then only mail() function is able to send the emails.

First we will talk about mail() function. You can pass the following parameters to mail function.

mail(to,subject,message,headers,parameters)

Sample code as follows:

<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php
$to = "support@purabtech.in/files/, wordpressapi@gmail.com";
$subject = "Test mail";
$message = "This is a simple email message.
 this is seond line.";
$from = "some@example.com, wordpressapi@gmail.com";

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $from";

$headers .= 'Cc: test@test1.com' . "\r\n";
$headers .= 'Bcc: test2@test1.com' . "\r\n";

mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>

If sendmail mail server is not configured on your server, mostly with windows server then there is issue.
But you can send email using anothere PHP programm called PHPmailer.
you can download the PHPmailer from following URL:

http://sourceforge.net/projects/phpmailer/

Download first PHPmailer and extract the phpmailer folder and in that folder you will find the following three files.
1. class.phpmailer.php
2. class.pop3.php
3. class.smtp.php

copy and paste the following files to your PHP application. and use the following code in case of use POP email

<?php
function sendmail_wordpressapi($to,$from,$subject,$body,$id){
//error_reporting(E_ALL);
error_reporting(E_STRICT);
date_default_timezone_set('America/Toronto');
include_once('class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail             = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "purabtech.in/files/"; // SMTP server
$mail->From       = $from;
$mail->FromName   = $from;
$mail->Subject    = $subject;
$mail->Body       = $body;
$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap   = 50; // set word wrap
$mail->MsgHTML($body);
$mail->AddAddress($to, $to);

if(!$mail->Send()) {
$mail             = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 465;                   // set the SMTP port for the GMAIL server
$mail->Username   = "WORDPRESSAPI@gmail.com";  // GMAIL username
$mail->Password   = "YOURPASSWORD";            // GMAIL password
$mail->AddReplyTo("smtp.production@gmail.com","wordpressapi");
$mail->From       = "support@purabtech.in/files/";
$mail->FromName   = "SMTP EMAIL-wordpressapi";
$mail->Subject    = "email server is having some issue";
$mail->Body       = "Hi,<br>email server is having some issue,<br> check this out";                      //HTML Body
$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap   = 50; // set word wrap
$mail->MsgHTML($body);
$mail->AddAddress("support@purabtech.in/files/", "support-wordpressapi");
$mail->IsHTML(true); // send as HTML
$mail->Send();

} else {
 echo "Message sent!";
}
}
?>

I created above function in this function I am using SMTP mail function for sending email but in case mail is not going using SMTP details.
you can also send emails using your gmail account details. you can edit or use this function as your requirement.

You can pass following parameters to sendmail_wordpressapi function:

sendmail_wordpressapi($to,$from,$subject,$body,$id);

You can send email to multiple email addresses. I am sure using above code sending emails through PHP is very easy. If you have any quries..write to me.