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,
    <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 />
  2. Save with the name connect.php

Step 4 : Create the GuestBook Form

  1. Type the following script,
    <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 />
  2. save with the name formguestbook.php

Step 5 : Create a Processing Script

  1. Type the following script,
    <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 />
  2. Save with the name savegb.php
  3. For the explanation, see script’s comment :D

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! :D
Hal yang juga menarik:

Hak Cipta

Semua skrip dan teknik dalam artikel di boleh digunakan sebagaimana kehendakmu tanpa perlu mencantumkan sumber. Kamu tidak boleh mengkopi seluruh artikel, dalam Bahasa Indonesia ataupun diterjemahkan ke dalam bahasa lain.

Related Articles:

Artikel terkait:


About the author

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 lo


Tinggalkan Balasan

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

x( X-( B-) ;-) :wink: :twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :arrow: :D :?: :-| :-x :-o :-P :-D :-? :-)) :-) :-( :-& :) :( :!: 8-O 8-) 8) (Русский) (yahoo) (worship) (woot) (wave) (unsure) (tongue) (thinking) (tears) (taser) (smileydance) (sleeping) (sick) (scenic) (rofl) (rock) (party) (panic) (okok) (nottalking) (ninja) (music) (muscle) (muhaha) (money) (mmm) (lonely) (lol) (lmao) (idiot) (hungry) (highfive) (heart_beat) (heart) (headspin) (hassle) (haha) (gym) (griltongue) (goodluck) (girlkiss) (funkydance) (fish_hit) (eyeroll) (evilsmirk) (evil_grin) (drinking) (doh) (devil) (dance) (cry) (cozy) (coffee) (brokenheart) (bringit) (blush) (bigeyes) (beer) (banana_rock) (banana_ninja) (banana_cool) (applause) (annoyed) (angry) (K) (: