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.

01CREATE TABLE IF NOT EXISTS `posts` (
02 
03`name` varchar(100) NOT NULL,
04 
05`email` varchar(100) NOT NULL,
06 
07`website` varchar(100) NOT NULL,
08 
09`message` text NOT NULL,
10 
11`ip` varchar(25) NOT NULL,
12 
13`date` varchar(50) NOT NULL
14 
15) 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:

01<code><code>echo "php?act=add' method='post'>"</code></code>
02 
03."Name:
04<input type='text' name='name' size='30' />
05"
06 
07."Email:
08<input type='text' name='email' size='30' />
09"
10 
11."Website:
12<input type='text' name='website' size='30' />
13"
14 
15."Message:
16<textarea cols='30' rows='8' name='message'>
17"
18 
19."<input type='submit' value='Post' />"
20 
21."</form>";

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

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

01if($_GET['act'] == "add") </code></code>
02 
03<code><code>{ // If act is "add" like this file.php?act=add display the content</code></code>
04 
05$sqlCon = mysql_connect("localhost""root""bananaman"); // Connect to database
06 
07if($sqlCon == true){ // If connection has been made then..
08 
09$sqlSel = mysql_select_db("guestbook"$sqlCon); // Select a database
10 
11if($sqlSel == true)
12 
13<code><code>{ // If database has been successfully selected gather info and post it</code></code>
14 
15$name addslashes(htmlspecialchars($_REQUEST['name'])); // Takes "name" field from the form
16 
17$email addslashes(htmlspecialchars($_REQUEST['email'])); // Takes "email" field from the form
18 
19$website addslashes(htmlspecialchars($_REQUEST['website'])); // Takes "website" field from the form
20 
21$message addslashes(htmlspecialchars($_REQUEST['message'])); // Takes "message" field from the form
22 
23$ip $_SERVER['REMOTE_ADDR']; // This takes user's IP
24 
25$time = time(); // Get UNIX timestamp
26 
27// This will create a SQL query which inserts all that information into database
28 
29$sql = "INSERT INTO posts (name, email, website, message, ip)
30 
31<code><code>VALUES ('".$name."''".$email."''".$website."', </code></code>
32 
33<code><code>'".$message."''".$ip."''".$time."')";</code></code>
34 
35// This will process SQL query and performs inserting info to database
36 
37$query = mysql_query($sql);
38 
39if($query == true) { // If everything was correct ..
40 
41echo "Your post has been successfully posted!"// Display a success message
42 
43}else // But if there was something wrong ..
44 
45echo "There was something wrong."// Display a fail message
46 
47// As you can see I have commented
48 
49// mysql_error() function. It is because
50 
51// if you want to see what went wrong with
52 
53// your SQL query. Displaying it is not very
54 
55// good idea because it's like a candy to
56 
57// hackers. So if you have fixed the problem
58 
59// make sure to add comment "//" in front of it
60 
61//echo mysql_error();
62 
63}
64 
65mysql_close($sqlCon); // Close connection to database
66 
67}else{
68 
69exit// If database selection wasn't a success close script
70 
71}
72 
73}else{
74 
75exit// If connecting to database wasn't a success close script
76 
77}
78 
79}

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

Step 3: Displaying Posts

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

Use this PHP code:

01<code><code>$sqlCon = mysql_connect("localhost""root""bananaman"); // Connect to database</code></code>
02 
03if($sqlCon == true) { // If connecting was successful ..
04 
05$sqlSel = mysql_select_db("guestbook"$sqlCon); // select your database
06 
07if($sqlSel == true) { // If database selecting was successful then take info from it
08 
09// Select all records from database
10 
11$sql "SELECT * FROM posts";
12 
13// Process your selection
14 
15$query = mysql_query($sql);
16 
17// This creates a table which contains all info
18 
19// of your posts
20 
21while($info = mysql_fetch_array($query)) {
22 
23echo "<table width='300' border='1'>"
24 
25."<tr>"
26 
27."<td>Posted by: <a href='".$info['website']."'>".$info['name']."</a> (<a href='mailto:".$info['email']."'>Email</a>)</td>"
28 
29."".date("H:i j F Y"$info['date']).""
30 
31."</tr>"
32 
33."<tr>"
34 
35."<td colspan='2'>".$info['message']."</td>"
36 
37."</tr>"
38 
39."</table>"
40 
41."
42";
43 
44}
45 
46mysql_close($sqlCon); // Close database connection
47 
48}else // If database selection wasn't successful ..
49 
50exit// exit form the script
51 
52}
53 
54}else // If connection to database wasn't success ..
55 
56exit// exit from your script
57 
58}

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

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.

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

Sample code as follows:

01<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php
02$to = "support@purabtech.in/files/, wordpressapi@gmail.com";
03$subject = "Test mail";
04$message = "This is a simple email message.
05 this is seond line.";
06$from = "some@example.com, wordpressapi@gmail.com";
07 
08// To send HTML mail, the Content-type header must be set
09$headers  = 'MIME-Version: 1.0' . "\r\n";
10$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
11$headers .= "From: $from";
12 
13$headers .= 'Cc: test@test1.com' . "\r\n";
14$headers .= 'Bcc: test2@test1.com' . "\r\n";
15 
16mail($to,$subject,$message,$headers);
17echo "Mail Sent.";
18?>

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

01<?php
02function sendmail_wordpressapi($to,$from,$subject,$body,$id){
03//error_reporting(E_ALL);
04error_reporting(E_STRICT);
05date_default_timezone_set('America/Toronto');
06include_once('class.phpmailer.php');
07//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
08$mail             = new PHPMailer();
09$mail->IsSMTP(); // telling the class to use SMTP
10$mail->Host       = "purabtech.in/files/"; // SMTP server
11$mail->From       = $from;
12$mail->FromName   = $from;
13$mail->Subject    = $subject;
14$mail->Body       = $body;
15$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
16$mail->WordWrap   = 50; // set word wrap
17$mail->MsgHTML($body);
18$mail->AddAddress($to, $to);
19 
20if(!$mail->Send()) {
21$mail             = new PHPMailer();
22$mail->IsSMTP();
23$mail->SMTPAuth   = true;                  // enable SMTP authentication
24$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
25$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
26$mail->Port       = 465;                   // set the SMTP port for the GMAIL server
27$mail->Username   = "WORDPRESSAPI@gmail.com"// GMAIL username
28$mail->Password   = "YOURPASSWORD";            // GMAIL password
29$mail->AddReplyTo("smtp.production@gmail.com","wordpressapi");
30$mail->From       = "support@purabtech.in/files/";
31$mail->FromName   = "SMTP EMAIL-wordpressapi";
32$mail->Subject    = "email server is having some issue";
33$mail->Body       = "Hi,<br>email server is having some issue,<br> check this out";                      //HTML Body
34$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
35$mail->WordWrap   = 50; // set word wrap
36$mail->MsgHTML($body);
37$mail->AddAddress("support@purabtech.in/files/", "support-wordpressapi");
38$mail->IsHTML(true); // send as HTML
39$mail->Send();
40 
41} else {
42 echo "Message sent!";
43}
44}
45?>

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.