PHP Tutorial: Guestbook for beginner

News No Comments »
I’m writting this php tutorial specially to all my fellow friends who wish to learn php but do not know where to start. Hope after this simple tutorial, you all will have a better understanding towards php. Below is the tutorial to develop a guestbook system. U might ask me why teach you all to write a guestbook system where guestbook almost dissappear on the internet. The answer is I want you all to learn the basic of php and i guestbook system is simple and good enough to demostrate all the commonly used php function.

This is just a tutorial, there are still having some security vulnerabilities.
DO NOT USE THESE SCRIPT FOR PRODUCTION PURPOSES!

Before i start i assume you all already has the WAMP or LAMP environment to test out the script. If you do not have PHP and MySQL install in your PC, you might want to take a look at my previous post "How to install AMP (apache, mysql and php) in window xp".

Let’s start:-

Before we start our code we need to design what we want our user to enter and how the data to be store in the database. For a basic guestbook it should allow user to enter their fullname, email, website and comments. So based on all this fields we come out with the table structure as below.

CREATE TABLE guestbook (
    id int(7) not null primary key auto_increment,
    fullname varchar(50) not null,
    email varchar(100),
    website varchar(100),
    comment text not null,
    created datetime not null
)

As you go thru the code, you will notice that ‘id’ and created are not mention earlier but now it’s included in our table structure. And, ‘id’ has been mark as the primary key here where it helps to identify each and every comment that visitor submit uniquely. Everytime user submit their comment we will create a new id for their comment at the same time we also save the date and time they submit their comment in ‘created’ field.

Copy and paste the code above and execute it in your mysql database. Once this script is executed, you will see a new table named ‘guestbook’ created

Ok now the database structure is ready, we can start our code now:-
First we need a form for user to key in their comment:-
Save this file as index.php

<html>
<head>
<title>Guestbook system</title>
</head>
<body>

<?php
    require_once(‘class.guestbook.php’);
    $g = new GuestBook();
   
    if($_POST[‘action‘] == ‘Submit‘) {
        // if user press on Submit button it will capture user input
        // and save into database table

        $g -> fullname = $_POST[‘fullname‘];
        $g -> email = $_POST[‘email‘];
        $g -> website = $_POST[‘website‘];
        $g -> comments = $_POST[‘comments‘];
        if(!$msg = $g -> save()) {
            $msg = ‘Your comment has been added successfully‘;
        }
        $msg .= ‘<br><br>‘;
    }

    $comments = $g -> getAllComments();
   
    if(!empty($comments)) {
        foreach($comments as $i => $val) {
            $content .= ‘<div style="border:1px solid #cccccc; background-color:#EBEBC2;">‘;
            $content .= $val[‘fullname‘] . ‘‘ . date(‘d M Y H:i‘, strtotime($val[‘created‘])) . ‘<br>‘;
            $content .= $val[‘email‘] . ‘<br>‘;
            $content .= $val[‘website‘] . ‘<br>‘;
            $content .= $val[‘comment‘] . ‘<br>‘;
            $content .= ‘</div><br>‘;
        }
    } else {
        $content = ‘No record in your guestbook.';
    }
    echo $msg;
    echo $content;
?>
<form name="form1" method="post" action="">
  Name:  <input type="text" name="fullname">
  <br>
  Email:  <input type="text" name="email">
  <br>
  Website:  <input type="text" name="website">
  <br>
  Comments:<br>  <textarea name="comments" cols="30" rows="5"></textarea><br>
  <input type="submit" name="action" value="Submit">
</form>
</body>
</html>

Then we will developed a class to store and retrieve all the data to and from the database.
replace your mysql_user and mysql_password with your username and password.
class.guestbook.php

<?php
    class GuestBook {
        var $id;
        var $fullname;
        var $email;
        var $website;
        var $comments;
        var $created;
       
        var $conn;
        var $query;
       
        function GuestBook() {
            $this -> conn = mysql_connect(‘localhost’, ‘mysql_user’, ‘mysql_password’) or die (‘Error: Could not connect to mysql server’);
            mysql_select_db(‘yourdatabase’, $this -> conn) or die (‘Error: Could not select db’);
        }
       
        function save() {
            if(!empty($this -> fullname) && !empty($this -> comments)) {
                $sql = "INSERT INTO guestbook (fullname, email, website, comment, created) VALUES (‘". $this -> fullname . "’, ‘" . $this -> email . "’, ‘" . $this -> website . "’, ‘" . $this -> comments . "’, ‘" . date(‘Y-m-d H:i:s’) . "’)";
                if(!$query = mysql_query($sql, $this -> conn)) {
                    return ‘Error, could not insert comment.<br>MySQL Error: ‘ . mysql_error();
                }
               
            } else {
                return ‘Error, paramenter is empty!';
            }
        }
       
        function getAllComments() {
            $sql = ‘SELECT * FROM guestbook';
            $query = mysql_query($sql);
           
            $i = 0;
            while($row = mysql_fetch_assoc($query)) {
                $ret[$i] = $row;
                $i++;
            }
           
            return $ret;
        }
    }
?>

After you create this 2 files and save in your web server root folder, you are basically done. Guestbook system is ready now. Open up your browser and enter http://localhost/yourdirectory/index.php and you can start to use the system.

I’ll explain the inside code next time. At this moment, try to go thru the code, i think it’s quite simple and easy to understand. I’ll post the code explaination next time. :)

The DOs and DON’Ts of Picking Up a Girl in the Computer Lab

Tech No Comments »

hahaha this is funny. Guys who are still in college or Uni, this is very useful for you while you are in the computer lab!

  • DO try to sound smart
  • DO dress for success
  • DO offer to go get what she printed
  • DO set the mood
  • DO look at Facebook
  • DO pick a good place to sit
  • DO leave your iPod conspicuously out next to the keyboard

  • DON’T look at porn
  • DON’T read over her shoulder
  • DON’T try to make out with her on the floor of the computer lab
  • DON’T throw shit at her
  • DON’T play online poker, you fucking asshole
  • DON’T let her catch you looking at porn
  • DON’T post to a blog

Click here to read more

Obelad gf’s birthday

My Life 3 Comments »

Yesterday is my obelad gf’s birthday. In the morning, we went to Aquaria KLCC to see the fishes swimming. I like fishes a lot but not my gf :p The place really nice for fish maniac like me. hehe. Although it’s nice but the entrance fee is a bit expensive. RM 28 person (with mycard) if i’m not mistake. We spend around 45 mins inside the Aquaria. After the Aquaria visit, obelad gf not feeling well. so end up go back home.

Actually i wanted to bring her to have Japanese Steambot buffet, but too bad she fall sick in the afternoon. So end up rest in my house. She slept for the whole afternoon and luckly she feel better after taking the good rest.

Since she fall sick, we went to have dinner at "Good Good steamboat" at Sri Petaling which just nearby her house. We reach there about 7.15pm and the restaurant already full of people, luckly we manage to get a seat. The food there is fresh and nice. if you got chance dun forget to visit this restaurant, their steambot is really really nice. My obelad gf like it a lot :)

Ok time to work :p
p/s: will put some nice pic here in the next post, all the pic at my gf’s cam

WP Theme
Entries RSS Comments RSS Log in