• 15Feb

    This is pretty much a basic “How to” for making a very basic guestbook that uses a database instead of text. I go over everything from making the database to the table, the html form and actual posting. If you watch this video you will pretty much see the basic idea of what a guestbook is, how they work, and how to make a basic one yourself.

    This video has two parts, so to understand fully, please watch both.


    Part 2 and the source after the jump

    <?php
    $link = mysql_connect('localhost', 'phpuser', 'phppass');
    if (!$link) {
        die('Not connected : ' . mysql_error());
    }else{
        //echo "I am connected. ";
    }
    
    // make foo the current db
    $db_selected = mysql_select_db('guestbook', $link);
    if (!$db_selected) {
        die ('Can\'t use guestbook DB : ' . mysql_error());
    }
    $name = trim($_REQUEST['name']);
    $comment = trim($_REQUEST['comment']);
    if(strlen($name)> 0){
        if(strlen($comment)> 0){
            $name = mysql_real_escape_string($name);
            $comment = mysql_real_escape_string($comment);
    
            $sql = "INSERT INTO `guestbook`.`entries` (
    `ID` ,
    `name` ,
    `date` ,
    `comment`
    )
    VALUES (
    NULL , '$name', UNIX_TIMESTAMP( ) , '$comment'
    );";
    mysql_query($sql);
        }else{
            echo "You did not put in a comment.";
        }
    }else{
        if(strlen($comment)> 0){
            echo "you got a comment, but not a name;";
        }
    }
    $sql = "SELECT *
    FROM `entries` ORDER BY `ID` DESC
    LIMIT 3";
    $result = mysql_query($sql);
    while($row = mysql_fetch_array($result)){
        echo $row['name']." Said: <blockquote>".$row['comment']."</blockquote><br />";
    }
    ?>
    <form action="?" method="post">
    Name:
    <input type="text" name="name" /><br />
    Comment: <br />
    <textarea name="comment"></textarea><br />
    <input type="submit" value="Submit" />
    </form>
    

    Posted by Kloplop321 @ 10:05 am

    Tags: , , , , , , , ,

4 Responses

WP_Orange_Techno
  • karol Says:

    nice one i check it too, btw when are u going to insert another tutorials Kloplop321? I am more flash dev….

  • Kloplop321 Says:

    Yes, I have not quit, I just have been very busy. In fact I plan to release some more very soon. I also ventured into being a flash dev, but I found some things limiting. However, I have made a flash-based login system(even with a captcha) that interacts with PHP a long long time ago.

  • lito Says:

    what am i doing wrong? i copy and change only the connection but when i tried it like your way, it doesn’t show the message and name when i hit submit button.

  • Kloplop321 Says:

    are you sure you have guestbook database fully working? That should only happen if you don’t have the MySQL working properly. Can you give my a mysql dump of your guestbook database?

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.