Thursday, February 6, 2014

Step by Step Mahir PHP 00003

Pada Kesempatan yang ketiga ini saya akan membahas aplikasi tambah, edit dan hapus dengan php. Aplikasi ini adalah contoh web dinamis dalam bahasa pemrograman php. Sebenarnya tutorial php dinamis sederhana memang sudah banyak. Tapi pada tutorial yang akan saya bahas ini terkait php untuk para pemula. Mungkin pada lain waktu saya akan menulis php dinamis tingkat lanjutan. Tapi sebagai mahasiswa yang tentunya mendalami tentang bahasa pemrograman tentu pikirannya akan terbuka. Mungkin contoh web dinamis ini agak berbelit-belit, tapi itulah logika sederhananya. Mungkin kalau anda sudah mahir insya Allah anda pasti bisa membuat beberapa link dalam satu file. Tanpa perlu berlama-lama mari kita mulai :

1. Buat dulu file databasenya dengan nama "blog" lalu tabel dengan nama "penduduk" :

2. Kalau sudah buat folder di webserver anda lalu buat file "index.php", berikut listing/script nya :
<?php
mysql_connect("localhost","root","");
mysql_select_db("blog");
echo "<h1>Data Penduduk</h1>
<form method=post action=form_input.php>
<input type=submit value='Tambah Data Penduduk'>
</form>
    <table border=1>
        <tr>
            <th>No</th>
            <th>Nama</th>
            <th>Alamat</th>
            <th>Aksi</th>
        </tr>";
    $tampil=mysql_query("select * from penduduk order by id");
    while($tampiltiapbaris=mysql_fetch_array($tampil)){
        echo "<tr>
                <td>$tampiltiapbaris[id]</td>
                <td>$tampiltiapbaris[nama]</td>
                <td>$tampiltiapbaris[alamat]</td>
                <td><a href=form_edit.php?id=$tampiltiapbaris[id]>Edit</a> | <a href=hapus.php?id=$tampiltiapbaris[id]>Hapus</a></td>
            </tr>";
    }
    echo "</table>";
?>

       
3. Setelah itu buat file "form_input.php", berikut scriptnya :
<?php
echo"<h2> Tambah Member</h2>
<form method=POST action=proses_input.php>
<table>
    <tr>
        <td>Nama</td>
        <td> : <input type=text name='nama'></td>
    </tr>
    <tr>
        <td>Alamat</td>
        <td> : <input type=text name='alamat'></td>
    </tr>
    <tr>
        <td colspan='2'>
            <input type=submit value=Simpan>
            <input type=button value=Batal onclick=self.history.back()>
        </td>
    </tr>
   
</table>
";
?>


4. Kalau udah selesai buat lagi aksinya "proses_input.php", berikut scriptnya :
<?php
mysql_connect("localhost","root","");
mysql_select_db("blog");

mysql_query("INSERT INTO penduduk(id,
                                nama,
                                alamat)
                         VALUES('$_POST[id]',
                                '$_POST[nama]',
                                '$_POST[alamat]')");
header('location:index.php');

?>


5. Kalau udah selesai buat lagi file "form_edit.php", berikut scriptnya :
<?php
mysql_connect("localhost","root","");
mysql_select_db("blog");
$dataedit=mysql_query("select * from penduduk where id='$_GET[id]'");
$hasil=mysql_fetch_array($dataedit);
echo"<h2> Edit Data Penduduk</h2>

<form method=POST action=proses_update.php>
<input type=hidden name=id value='$hasil[id]'>
<table>
    <tr>
        <td>Nama</td>
        <td> : <input type=text name='nama' value='$hasil[nama]'></td>
    </tr>
    <tr>
        <td>Alamat</td>
        <td> : <input type=text name='alamat' value='$hasil[alamat]'></td>
    </tr>
    <tr>
        <td colspan='2'>
            <input type=submit value=Simpan>
            <input type=button value=Batal onclick=self.history.back()>
        </td>
    </tr>
   
</table>
";
?>


6. Kalau udah selesai, buat lagi file aksinya "proses_update.php", berikut adalah scriptnya :
<?php
mysql_connect("localhost","root","");
mysql_select_db("blog");

mysql_query("UPDATE penduduk set nama='$_POST[nama]', alamat='$_POST[alamat]' where id='$_POST[id]'");
header('location:index.php');

?>

7. Terakhir Buat aksi hapus "hapus.php", berikut adalah scriptnya :
<?php
mysql_connect("localhost","root","");
mysql_select_db("blog");

mysql_query("delete from penduduk where id='$_GET[id]'");
header('location:index.php');

?>


Selamat mencoba...!!!!
Comments
0 Comments

No comments:

Post a Comment