Rabu, 10 Juni 2015

Tugas Praktikum ke 4

Table ‘tb_mhs’ di database ‘praktikum_daskom’

Program Web sesuai ketentuan modul

<!DOCTYPE html>
<html>
<head>
 <title>DATABASE PRAKTIKUM DASKOM</title>
</head>
<body>
 <center>
  <h1>SELAMAT DATANG</h1>
  <hr width="90%" size="4" color=#000000 noshade />
  <ol start="1"> 
   <li><a style="text-decoration:none; color:black" href="input_data.php">INPUT DATA MAHASISWA</a></li>
   <li><a style="text-decoration:none; color:black" href="lihat_data.php">LIHAT DATA MAHASISWA</a></li>
  </ol> 
  <hr width="90%" size="4" color=#000000 noshade />
 </center>
</body>
</html>

File koneksi.php :

<?php
 $konek = mysql_connect("localhost", "root", "") or die("Koneksi ke server gagal");
 mysql_select_db("praktikum_daskom",$konek) or die ("Database tidak ditemukan");
?>

B. SAAT DIKLIK INPUT DATA MAHASISWA (input_data.php)

<!DOCTYPE html>
<?php
 if(isset($_POST['simpan'])){
  include "koneksi.php";
  $nbi = $_POST['nbi'];
  $nama = $_POST['nama'];
  $kelas = $_POST['kelas'];
  $alamat = $_POST['alamat'];
  $query = "INSERT INTO `tb_mhs` (nbi, nama, kelas, alamat) VALUES ('".$nbi."','".$nama."','".$kelas."','".$alamat."')";
  $query_sukses = mysql_query($query);
  if($query_sukses){
   header("location:lihat_data.php");
  }else{
   echo(
    '<script>
     alert("Gagal menambah Data, NBI sudah ada!")
     location.href = "lihat_data.php"
    </script>'
   );
  }
 }
?>
<html>
<head>
 <title>FORM PENDAFTARAN PRAKTIKUM</title>
</head>
<body>
 <center>
  <hr width="90%" size="4" color=#000000 noshade />
  <li><h1>INPUTKAN DATA ANDA</h1></li>
  <hr width="90%" size="4" color=#000000 noshade />
  <form action="input_data.php" method="post">
   <table>
    <tr>   
     <td>NBI</td>
     <td width="30%"></td>
     <td><input type="text" name="nbi" size="20" maxlength="9"/></td>
    </tr>
    <tr>   
     <td>Nama</td>
     <td width="30%"></td>
     <td><input type="text" name="nama" size="20" maxlength="20"/></td>
    </tr>
    <tr>   
     <td>Kelas</td>
     <td width="30%"></td>
     <td><input type="text" name="kelas" size="20" maxlength="30"/></td>
    </tr>
    <tr>   
     <td>Alamat</td>
     <td width="30%"></td>
     <td><input type="text" name="alamat" size="20" maxlength="30"/></td>
    </tr>
    <tr>   
     <td colspan="2">
      <input type="submit" name="simpan" value="SIMPAN">
      <input type="reset" name="reset" value="RESET">
     </td>
    </tr>    
   </table>
  </form>
 </center>
</body>
</html>

C. SAAT DIKLIK SIMPAN, MAKA DATA OTOMATIS BERTAMBAH (lihat_data.php)

<!DOCTYPE html>
<html>
<head>
 <title>DATA PESERTA PRAKTIKUM</title>
</head>
<body>
 <center>
  <hr width="90%" size="4" color=#000000 noshade />
  <li><h1>DATA NAMA PRAKTIKUM</h1></li>
  <hr width="90%" size="4" color=#000000 noshade />
  <table border="1" align="center">
  <tr>
   <th>NBI</th>
   <th>NAMA</th>
   <th>ALAMAT</th>
   <th colspan="2">EKSEKUSI</th>
  </tr>
<?php
  include "koneksi.php";
  $data=mysql_query("SELECT * FROM tb_mhs");
  while($hasil=mysql_fetch_array($data)){
?>
   <tr>
    <td><?php echo $hasil['NBI']; ?></td>
    <td><?php echo $hasil['NAMA']; ?></td>
    <td><?php echo $hasil['KELAS']; ?></td>
    <td><?php echo $hasil['ALAMAT']; ?></td>
    <td colspan="2">
     <a href="edit.php?NBI=<?php echo $hasil['NBI']; ?>">edit</a>
     /
     <a href="delete.php?pilih=0&NBI=<?php echo $hasil['NBI']; ?>">hapus</a>
    </td>
   </tr>
<?php
  };
?>
  </table>
 <a href="input_data.php">Tambah Data</a>
 <a href="index.html">Kembali</a>
 </center>
</body>
</html>

D. DATA OTOMATIS TERSIMPAN PADA DATABASE

E. SAAT DIKLIK EDIT (edit.php)

<!DOCTYPE html>
<?php
 if(isset($_POST['edit'])){
  include "koneksi.php";
  $nbi = $_POST['nbi'];
  $nama = $_POST['nama'];
  $kelas = $_POST['kelas'];
  $alamat = $_POST['alamat'];
  $query ="UPDATE `tb_mhs` SET 
     NBI='".$nbi."',
     NAMA='".$nama."',
     KELAS='".$kelas."',
     ALAMAT='".$alamat."'
    WHERE NBI='".$_GET['NBI']."'";
  $query_sukses = mysql_query($query);
  if($query_sukses){
   header("location:lihat_data.php");
  }else{
   echo(
    '<script>
     alert("Gagal Mengedit Data, NBI sudah ada!")
     location.href = "lihat_data.php"
    </script>'
   );
  }
 }else{
  include "koneksi.php";
  $data=mysql_query("SELECT * FROM tb_mhs WHERE NBI='".$_GET['NBI']."'");
  $hasil=mysql_fetch_array($data);
 }
?>
<html>
<head>
 <title>FORM EDIT DATA PESERTA PRAKTIKUM</title>
</head>
<body>
 <center>
  <li><h1>SILAHKAN EDIT DATA ANDA</h1></li>
  <form action="edit.php?NBI=<?php echo $_GET['NBI']; ?>" method="post">
   <table>
    <tr>   
     <td>NBI</td>
     <td width="30%"></td>
     <td><input type="text" name="nbi" size="20" maxlength="9" value="<?php echo $hasil['NBI'];?>" /></td>
    </tr>
    <tr>   
     <td>Nama</td>
     <td width="30%"></td>
     <td><input type="text" name="nama" size="20" maxlength="20" value="<?php echo $hasil['NAMA'];?>" /></td>
    </tr>
    <tr>   
     <td>Kelas</td>
     <td width="30%"></td>
     <td><input type="text" name="kelas" size="20" maxlength="30" value="<?php echo $hasil['KELAS'];?>" /></td>
    </tr>
    <tr>   
     <td>Alamat</td>
     <td width="30%"></td>
     <td><input type="text" name="alamat" size="20" maxlength="30" value="<?php echo $hasil['ALAMAT'];?>" /></td>
    </tr>
    <tr>   
     <td colspan="2">
      <input type="submit" name="edit" value="SIMPAN">
      <input type="reset" name="reset" value="RESET">
     </td>
    </tr>    
   </table>
  </form>
 </center>
</body>
</html>

F. SETELAH DIEDIT DAN DISIMPAN

G. SAAT DIKLIK DELETE (delete.php)

<?php
 if ($_GET['pilih']==0){
?>
 <script>
  var tanya = confirm("APAKAH ANDA YAKIN MENGHAPUS DATA DENGAN NBI = '<?php echo $_GET['NBI']; ?>' ")
  if (tanya==true){
   location.href = "delete.php?pilih=1&NBI=<?php echo $_GET['NBI']; ?>";
  }else{
   location.href = "lihat_data.php";
  }
 </script>
<?php
 }else{
  include "koneksi.php";
  mysql_query("DELETE FROM tb_mhs WHERE NBI='".$_GET['NBI']."'") or die ("Data gagal dihapus");
  header("location:lihat_data.php");
 }
?>

NB :
- SAAT DIKLIK TAMBAH DATA MAKA AKAN MENUJU KE FILE input_data.php
- SAAT DIKLIK KEMBALI MAKA AKAN KEMBALI KE MENU AWAL (index.html)

Tidak ada komentar:

Posting Komentar