
Buatlah database dengan nama test, dengan nama tabel costumer

Buat file koneksi.php
1 2 3 4 | <? php $ conn = mysql_connect ('localhost','root',''); mysql_select_db('test',$conn); ?> |
Buat form input atau tambah data baru.
costumer.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | < div class = "formset" > < form method = "post" > < table align = "center" > < tr >< td colspan = "2" align = "center" >< h2 >Data Customer</ h2 ></ td ></ tr > < tr > < td width = "137" >Id Customer</ td >< td width = "353" >< input type = "text" name = "ic" size = "10" maxlength = "10" id = "ic" /></ td ></ tr > < tr > < td >Nama Customer</ td >< td >< input type = "text" name = "nc" size = "50" maxlength = "50" id = "nc" /></ td ></ tr > < tr > < td >Jurusan</ td >< td >< input type = "text" name = "jr" size = "50" maxlength = "50" id = "nc" /></ td ></ tr > < tr >< td height = "38" ></ td >< td >< input name = "Simpan" type = "submit" value = "Simpan" /> < input type = "reset" value = "Reset" /></ td ></ tr > </ table > </ form > </ div > |
Buat form update data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | < div class = "formset" > < form method = "post" > < table align = "center" > < tr >< td colspan = "2" align = "center" >< h2 >Data Customer</ h2 ></ td ></ tr > < tr > < td width = "137" >Id Customer</ td >< td width = "353" >< input name = "ic" type = "text" id = "ic" value="<?php echo $xx[0] ?>" size="10" maxlength="10" readonly /></ td ></ tr > < tr > < td >Nama Customer</ td >< td >< input name = "nc" type = "text" id = "nc" value="<?php echo $xx[1] ?>" size="50" maxlength="50"/></ td ></ tr > < tr > < td >Jurusan</ td >< td >< input name = "jr" type = "text" id = "nc" value="<?php echo $xx[2] ?>" size="50" maxlength="50"/></ td ></ tr > < tr >< td height = "38" ></ td >< td >< input name = "update" type = "submit" value = "Update" /> < input type = "submit" value = "Reset" name = "reset" /></ td ></ tr > </ table > </ form > </ div > |
Untuk menggunakan salah satu form kita gunakan if.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <? php include 'koneksi.php'; if (isset($_GET['nocus'])){ $nokam=$_GET['nocus']; $ sa = mysql_query ("select *from customer where id_costumer = '$nokam' ",$conn); $ xx = mysql_fetch_array ($sa); ?> //masukkan form update data. <? php } else { ?> //masukkan form input/add data baru <?php } |
Untuk koding proses simpan dan update data pastekan koding dibawah ini.
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 | if($_SERVER['REQUEST_METHOD'] == "POST"){ if (isset($_POST['reset'])){ echo "< meta http-equiv = 'refresh' content = '0; url=?p=customer' >"; exit; } $id = $_POST['ic']; $nama = $_POST['nc']; $jr = $_POST['jr']; if ($id=="" || $nama=="" || $jr=="" ){ echo "< script >alert('Data belum lengkap...')</ script >"; } else { if (isset($_POST['Simpan'])){ $queri="insert into customer values ('$id','$nama','$jr')"; mysql_query($queri,$conn); echo "< script >alert('Data sudah disimpan...')</ script >"; } else if (isset($_POST['update'])){ $queri="update customer set nama_costumer='$nama',jurusan='$jr' where id_costumer='$id'"; mysql_query($queri,$conn); echo "< meta http-equiv = 'refresh' content = '0; url=?p=customer' >"; echo "< script >alert('Data sudah diupdate...')</ script >"; } } } ?> |
Selanjutnya menampilkan data yang tersimpan kedalam sebuah tabel, pastekan koding berikut pada bari paling bawah.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | < table width = "70%" border = "1" align = "center" class = "tabel" > < tr >< th width = "9%" >No</ th > < th width = "15%" >Id Customer</ th > < th width = "36%" >Nama Customer</ th > < th width = "26%" >Jurusan</ th > < th width = "20%" colspan = "2" >Modify</ th > </ tr > <? php $ queri = "select *from customer" ; $ hasil = mysql_query ($queri); $ jm = mysql_num_rows ($hasil); $ no = 1 ; while ($ data = mysql_fetch_array ($hasil)){ echo "<tr>< td align = 'center' >$no</ td >< td align = 'center' >$data[0]</ td >< td >$data[1]</ td >< td >$data[2]</ td >< td >< a href = '?p=customer&nocus=$data[0]' >edit < a href = 'hapus.php?nocus=$data[0]' onClick=\"return confirm('Anda yakin ingin menghapus $data[1] ?')\">hapus</ a ></ td ></ tr >"; $no++; } ?> </ table > |
Koding lengkapnya adalah sebagai berikut
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | <? php include 'koneksi.php'; if (isset($_GET['nocus'])){ $nokam=$_GET['nocus']; $ sa = mysql_query ("select *from customer where id_costumer = '$nokam' ",$conn); $ xx = mysql_fetch_array ($sa); ?> < div class = "formset" > < form method = "post" > < table align = "center" > < tr >< td colspan = "2" align = "center" >< h2 >Data Customer</ h2 ></ td ></ tr > < tr > < td width = "137" >Id Customer</ td >< td width = "353" >< input name = "ic" type = "text" id = "ic" value="<?php echo $xx[0] ?>" size="10" maxlength="10" readonly /></ td ></ tr > < tr > < td >Nama Customer</ td >< td >< input name = "nc" type = "text" id = "nc" value="<?php echo $xx[1] ?>" size="50" maxlength="50"/></ td ></ tr > < tr > < td >Jurusan</ td >< td >< input name = "jr" type = "text" id = "nc" value="<?php echo $xx[2] ?>" size="50" maxlength="50"/></ td ></ tr > < tr >< td height = "38" ></ td >< td >< input name = "update" type = "submit" value = "Update" /> < input type = "submit" value = "Reset" name = "reset" /></ td ></ tr > </ table > </ form > </ div > <? php } else { ?> < div class = "formset" > < form method = "post" > < table align = "center" > < tr >< td colspan = "2" align = "center" >< h2 >Data Customer</ h2 ></ td ></ tr > < tr > < td width = "137" >Id Customer</ td >< td width = "353" >< input type = "text" name = "ic" size = "10" maxlength = "10" id = "ic" /></ td ></ tr > < tr > < td >Nama Customer</ td >< td >< input type = "text" name = "nc" size = "50" maxlength = "50" id = "nc" /></ td ></ tr > < tr > < td >Jurusan</ td >< td >< input type = "text" name = "jr" size = "50" maxlength = "50" id = "nc" /></ td ></ tr > < tr >< td height = "38" ></ td >< td >< input name = "Simpan" type = "submit" value = "Simpan" /> < input type = "reset" value = "Reset" /></ td ></ tr > </ table > </ form > </ div > <? php } if($_SERVER['REQUEST_METHOD'] == "POST"){ if (isset($_POST['reset'])){ echo "<meta http-equiv = 'refresh' content = '0; url=?p=customer' >"; exit; } $id = $_POST['ic']; $nama = $_POST['nc']; $jr = $_POST['jr']; if ($id=="" || $nama=="" || $jr=="" ){ echo "< script >alert('Data belum lengkap...')</ script >"; } else { if (isset($_POST['Simpan'])){ $queri="insert into customer values ('$id','$nama','$jr')"; mysql_query($queri,$conn); echo "< script >alert('Data sudah disimpan...')</ script >"; } else if (isset($_POST['update'])){ $queri="update customer set nama_costumer='$nama',jurusan='$jr' where id_costumer='$id'"; mysql_query($queri,$conn); echo "< meta http-equiv = 'refresh' content = '0; url=?p=customer' >"; echo "< script >alert('Data sudah diupdate...')</ script >"; } } } ?> < table width = "70%" border = "1" align = "center" class = "tabel" > < tr >< th width = "9%" >No</ th > < th width = "15%" >Id Customer</ th > < th width = "36%" >Nama Customer</ th > < th width = "26%" >Jurusan</ th > < th width = "20%" colspan = "2" >Modify</ th > </ tr > <? php $ queri = "select *from customer" ; $ hasil = mysql_query ($queri); $ jm = mysql_num_rows ($hasil); $ no = 1 ; while ($ data = mysql_fetch_array ($hasil)){ echo "<tr>< td align = 'center' >$no</ td >< td align = 'center' >$data[0]</ td >< td >$data[1]</ td >< td >$data[2]</ td >< td >< a href = '?p=customer&nocus=$data[0]' >edit < a href = 'hapus.php?nocus=$data[0]' onClick=\"return confirm('Anda yakin ingin menghapus $data[1] ?')\">hapus</ a ></ td ></ tr >"; $no++; } ?> </ table > |
Buat file hapus.php
hapus.php
1 2 3 4 5 6 | <? php if (isset($_GET['nocus'])){ mysql_query("delete from customer where id_costumer = '$_GET[nocus]' "); echo '<meta http-equiv = "refresh" content = "0;URL=customer.php" >'; } ?> |
Hasilnya seperti dibawah ini.

No comments:
Post a Comment