Buku tamu yang akan kita buat nantinya, memiliki fitur post saja. Mungkin kedepannya saya akan tambahkan fitur post, edit, dan delete. Namun biar lebih gampang sekarang kita hanya akan membahas fitur post saja.
Oke kita langsung saja ke langkah-langkahnya yuk…
1. buat database baru di phpmyadmin, namanya terserah anda, namun disini saya memberi nama ‘guestbook’ (tanpa tanda petik), setelah buat database baru, buat juga table baru. beri nama table tersebut terserah anda. Keterangan Table nya adalah sebagai berikut :
1
2
3
4
5
6
7
8
9
10
11
| <?php$host = 'localhost';$user = 'root';$pass = '';$db = 'guestbook';$con = mysql_connect($host,$user,$pass); if (!$con) { echo "Gagal Konek database".mysql_error(); }mysql_select_db($db); |
Save script diatas dengan nama connect.php
3. Buat halaman index.php. Fungsi dari halaman ini sebagai form untuk mengisi bukutamu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| <html> <head> <title>Guest Book</title> </head> <body> <h2>Guest Book </h2> <form method="post" action="direct_post.php"> <table> <tr> <td>Name</td> <td><input type="text" name="user"></td> </tr> <tr> <td>email</td> <td><input type="text" name="email"></td> </tr> <tr> <td>Comment</td> <td><textarea cols="40" rows="5" name="comment"></textarea></td> </tr> <tr> <td></td> <td><input type="submit" value="comment"></td> </tr> </table> </form> </body></html> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| <?php// Connect ke Databaseinclude 'connect.php';$user = $_POST['user'];$email = $_POST['email'];$comment = $_POST['comment'];//Check Form Kosongif (empty($user) || empty($email) || empty($comment)) { echo "Form ada yang kosong, silahkan isi ulang"; }else { $query = mysql_query("insert into komentar set user = '$user', email = '$email', comment = '$comment'"); if ($query == TRUE) { echo "Data Berhasil ditambah, silahkan lihat daftar <a href='comment.php'>komentar</a>"; } else { echo "error"; } } |
5. Membuat comment.php yang dimana halaman tersebut berfungsi sebagai menampilkan daftar komentar yang telah di input ke database
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| <html><head><title>Show Comment</title></head><body><h2>Show Comment</h2><table border="1px" width="50%"><tr><td>Nama</td><td>Komentar</td></tr><?phpinclude 'connect.php';$query = mysql_query("select * from komentar"); //menyortir data dari databasewhile ($data=mysql_fetch_array($query)) // looping dan mengamil data komentar dari database{?><tr><td><a href="mailto:<?php echo $data['email'];?>"><?php echo $data['user'];?></a></td><td><?php echo $data['comment'];?></td></tr><?php}?></table><a href="index.php">Berikan Komentar</a></body></html> |
Jika anda malas mengikuti tutorialnya, anda bisa download script yang saya buat ini disin