Kategori
PHP

Simple Guest Book with PHP & Mysql

Case Study : Create simple guest book with PHP Mysql
Requirements : Webserver Packages, already installed.

Ok, lets just practices.

Step 1 : Prepare the Database

  1. Create database named db_tutorial
  2. Prepare tabel named tb_guestbook, with structure like below.
  3. Done!

Step 2 : Prepare the Work Folder

  1. Create folder named tutorphp in your document root
  2. Save all the files in this tutorial in this folder.

Step 3 : Create script to connect to DB

  1. Type the following script,
    [sourcecode language=”php”]<br />
    &lt;?php<br />
    $host = &quot;localhost&quot;;<br />
    $user = &quot;root&quot;;//adjust according to your mysql setting<br />
    $pass = &quot;&quot;; //adjust according to your mysql setting, i use no password here<br />
    $dbName = &quot;db_tutorial&quot;;<br />
    mysql_connect($host, $user, $pass);<br />
    mysql_select_db($dbName)<br />
    or die (&quot;Connect Failed !! : &quot;.mysql_error());<br />
    ?&gt;<br />
    [/sourcecode]
  2. Save with the name connect.php

Step 4 : Create the GuestBook Form

  1. Type the following script,
    [sourcecode language=”php”]<br />
    &lt;h1&gt;Guestbook&lt;/h1&gt;<br />
    &lt;form method=&quot;post&quot; action=&quot;savegb.php&quot;&gt;<br />
    &lt;table&gt;<br />
    &lt;tr&gt;&lt;td&gt;Your Name&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;<br />
    &lt;tr&gt;&lt;td&gt;Your Email&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;<br />
    &lt;tr&gt;&lt;td&gt;Your Comment&lt;/td&gt;&lt;td&gt;&lt;textarea name=&quot;comment&quot;&gt;&lt;/textarea&gt;&lt;/td&gt;&lt;/tr&gt;<br />
    &lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;<br />
    &lt;/table&gt;<br />
    &lt;/form&gt;</p>
    <p>[/sourcecode]
  2. save with the name formguestbook.php

Step 5 : Create a Processing Script

  1. Type the following script,
    [sourcecode language=”php”]<br />
    &lt;?php<br />
    include ‘connect.php’;</p>
    <p>$name = $_POST[‘name’]; //get the name value from form<br />
    $email = $_POST[‘email’]; //get the email value from form<br />
    $comment = $_POST[‘comment’]; //get the comment value from form</p>
    <p>//query to save to database<br />
    $q1 = &quot;INSERT into tb_guestbook value (”,’$name’,’$email’,’$comment’)&quot;;<br />
    $result1 = mysql_query($q1);</p>
    <p>//query to get data from database<br />
    $q2 = ‘SELECT * FROM tb_guestbook order by id desc’;<br />
    $result2 = mysql_query($q2);</p>
    <p>//show the data from database<br />
    while ($record = mysql_fetch_array($result2)) {<br />
    echo &quot;&lt;table&gt;&quot;;<br />
    echo &quot;&lt;tr&gt;&lt;td&gt;Name&lt;/td&gt;&lt;td&gt;:&lt;/td&gt;&lt;td&gt;&quot;.$record[‘name’].&quot;&lt;/td&gt;&lt;/tr&gt;&quot;;<br />
    echo &quot;&lt;tr&gt;&lt;td&gt;Email&lt;/td&gt;&lt;td&gt;:&lt;/td&gt;&lt;td&gt;&quot;.$record[‘email’].&quot;&lt;/td&gt;&lt;/tr&gt;&quot;;<br />
    echo &quot;&lt;tr&gt;&lt;td&gt;Comment&lt;/td&gt;&lt;td&gt;:&lt;/td&gt;&lt;td&gt;&quot;.$record[‘comment’].&quot;&lt;/td&gt;&lt;/tr&gt;&quot;;<br />
    echo &quot;&lt;/table&gt;&quot;;<br />
    echo &quot;&lt;br/&gt;&quot;;<br />
    }<br />
    ?&gt;<br />
    [/sourcecode]
  2. Save with the name savegb.php
  3. For the explanation, see script’s comment šŸ˜€

Step 6 : Testing Code

  1. Go to http://localhost/tutorphp/formguestbook.php. You will see the form like below. Fill the form (the example is like the picture below).
  2. Click save and you will see the result.

OK. Haaaaaaaapppy Coding! šŸ˜€

Oleh littleflow3r

obviously, a girl. with sweet smile, off course. turning her 21 years trapped on Informatics departement and just started to having some crush with it lately. she uses wordpress. she loves php. she loves to satisfied her shopping hobbies just via looking around in online shop. oh yeah, she got an A for her english subject^^

3 replies on “Simple Guest Book with PHP & Mysql”

assalammualaikum…thank’s so much
tolong dong kirim ke email saya …materi2 nya bagus2….langganan nih… sya masih buta script…suka maen game mulu…eh pas liat web ini…alhamdulillahjadi ada keinginan belajar.. php yes game no. thank’s B-)

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

This site uses Akismet to reduce spam. Learn how your comment data is processed.