UTS PWEB 17 Oktober 2018

Berikut adalah code SQLnya.
 /*==============================================================*/  
 /* DBMS name:   MySQL 5.0                  */  
 /* Created on:   10/17/2018 8:57:30 AM            */  
 /*==============================================================*/  
 drop table if exists KELAS;  
 drop table if exists PRESTASI;  
 drop table if exists SISWA;  
 /*==============================================================*/  
 /* Table: KELAS                         */  
 /*==============================================================*/  
 create table KELAS  
 (  
   KODEKELAS      char(3) not null,  
   primary key (KODEKELAS)  
 );  
 /*==============================================================*/  
 /* Table: PRESTASI                       */  
 /*==============================================================*/  
 create table PRESTASI  
 (  
   IDPRESTASI      int not null,  
   NOINDUK       int not null,  
   JENISPRESTASI    bool not null,  
   KETERANGAN      varchar(100) not null,  
   primary key (IDPRESTASI)  
 );  
 /*==============================================================*/  
 /* Table: SISWA                         */  
 /*==============================================================*/  
 create table SISWA  
 (  
   NOINDUK       int not null,  
   KODEKELAS      char(3) not null,  
   NAMASISWA      varchar(50) not null,  
   primary key (NOINDUK)  
 );  
 alter table PRESTASI add constraint FK_PRESTASI foreign key (NOINDUK)  
    references SISWA (NOINDUK) on delete restrict on update restrict;  
 alter table SISWA add constraint FK_KELAS foreign key (KODEKELAS)  
    references KELAS (KODEKELAS) on delete restrict on update restrict;  

Berikut adalah source code dari setiap file php.

config.php
 <?php  
 $server = "localhost";  
 $user = "root";  
 $password = "";  
 $nama_database = "prestasi_siswa";  
 $db = mysqli_connect($server, $user, $password, $nama_database);  
 if( !$db ){  
   die("Gagal terhubung dengan database: " . mysqli_connect_error());  
 }  
 ?>  

index.php
 <!DOCTYPE html>  
 <html>  
 <head>  
   <title>Index Prestasi Siswa</title>  
 </head>  
 <body>  
   <header>  
     <h3>Data Prestasi Siswa</h3>  
     <h1>SMA Jaya Makmur</h1>  
   </header>  
   <h4>Menu</h4>  
   <nav>  
     <ul>  
       <li><a href="form-tambah-prestasi.php">Tambah Prestasi</a></li>  
       <li><a href="list-prestasi.php">List Prestasi</a></li>  
     </ul>  
   </nav>  
   </body>  
 </html>  

list-prestasi.php
 <?php include("config.php"); ?>  
 <!DOCTYPE html>  
 <html>  
 <head>  
   <title>Index Prestasi Siswa</title>  
 </head>  
 <body>  
   <header>  
     <h3>Prestasi yang diraih</h3>  
   </header>  
   <nav>  
     <a href="form-tambah-prestasi.php">[+] Tambah Baru</a>  
   </nav>  
   <br>  
   <table border="1">  
   <thead>  
     <tr>  
       <th>No</th>  
       <th>No. Induk</th>  
       <th>Nama</th>  
       <th>Kelas</th>  
       <th>Jenis Prestasi</th>  
       <th>Keterangan</th>  
       <th>Tindakan</th>  
     </tr>  
   </thead>  
   <tbody>  
     <?php  
     $sql = "SELECT * FROM prestasi";  
     $query = mysqli_query($db, $sql);  
     $nomer = 0;  
     while($prestasi = mysqli_fetch_array($query)){  
       $sql0 = "SELECT * FROM siswa";  
       $query0 = mysqli_query($db, $sql0);  
       while($siswa = mysqli_fetch_array($query0)){  
         if ($siswa['NOINDUK']==$prestasi['NOINDUK']){  
           break;  
         }  
       }  
       echo "<tr>";  
       $nomer++;  
       echo "<td>".$nomer."</td>";  
       echo "<td>".$prestasi['NOINDUK']."</td>";  
       echo "<td>".$siswa['NAMASISWA']."</td>";  
       echo "<td>".$siswa['KODEKELAS']."</td>";  
       if ($prestasi['JENISPRESTASI']==0) {  
         echo "<td>"."Akademik"."</td>";  
       }  
       else{  
         echo "<td>"."Non-Akademik"."</td>";  
       }  
       echo "<td>".$prestasi['KETERANGAN']."</td>";  
       echo "<td>";  
       echo "<a href='form-edit.php?id=".$prestasi['IDPRESTASI']."'>Edit</a> | ";  
       echo "<a href='hapus.php?id=".$prestasi['IDPRESTASI']."'>Hapus</a>";  
       echo "</td>";  
       echo "</tr>";  
     }  
     ?>  
   </tbody>  
   </table>  
   <p>Total: <?php echo mysqli_num_rows($query) ?></p>  
   </body>  
 </html>  

form-tambah-prestasi.php
 <!DOCTYPE html>  
 <html>  
 <head>  
   <title>Formulir Penambahan Index Prestasi</title>  
 </head>  
 <body>  
   <header>  
     <h3>Formulir Penambahan Index Prestasi</h3>  
   </header>  
   <form action="proses-penambahan.php" method="POST">  
     <fieldset>  
     <p>  
       <label for="nama">Nomor Induk: </label>  
       <input type="number" name="nomorinduk"/>  
     </p>  
     <p>  
       <label>Prestasi yang diraih:</label>  
         Akademik <input type="radio" name="jenis_prestasi" id="jenis_prestasi" value="0">  
         Non-Akademik <input type="radio" name="jenis_prestasi" id="jenis_prestasi" value="1">   
     </p>  
     <p>  
       <label for="sekolah_asal">Keterangan: </label>  
       <input type="text" name="keterangan"/>  
     </p>  
     <p>  
       <input type="submit" value="Tambah" name="tambah" />  
     </p>  
     </fieldset>  
   </form>  
   </body>  
 </html>  

proses-penambahan.php
 <?php  
 include("config.php");  
 // cek apakah tombol daftar sudah diklik atau blum?  
 if(isset($_POST['tambah'])){  
   // ambil data dari formulir  
   $induk = $_POST['nomorinduk'];  
   $jenis = $_POST['jenis_prestasi'];  
   $ket = $_POST['keterangan'];  
   // buat query  
   $sql = "INSERT INTO prestasi (NOINDUK, JENISPRESTASI, KETERANGAN) VALUE ('$induk', '$jenis', '$ket')";  
   $query = mysqli_query($db, $sql);  
   // apakah query simpan berhasil?  
   if( $query ) {  
     // kalau berhasil alihkan ke halaman index.php dengan status=sukses  
     header('Location: index.php?status=sukses');  
   } else {  
     // kalau gagal alihkan ke halaman indek.php dengan status=gagal  
     header('Location: index.php?status=gagal');  
   }  
 } else {  
   die("Akses dilarang...");  
 }  
 ?>  

form-edit.php
 <?php  
 include("config.php");  
 // kalau tidak ada id di query string  
 if( !isset($_GET['id']) ){  
   header('Location: list-prestasi.php');  
 }  
 //ambil id dari query string  
 $id = $_GET['id'];  
 // buat query untuk ambil data dari database  
 $sql = "SELECT * FROM prestasi WHERE IDPRESTASI=$id";  
 $query = mysqli_query($db, $sql);  
 $prestasi = mysqli_fetch_assoc($query);  
 // jika data yang di-edit tidak ditemukan  
 if( mysqli_num_rows($query) < 1 ){  
   die("data tidak ditemukan...");  
 }  
 ?>  
 <!DOCTYPE html>  
 <html>  
 <head>  
   <title>Formulir Edit Prestasi</title>  
 </head>  
 <body>  
   <header>  
     <h3>Formulir Edit Prestasi</h3>  
   </header>  
   <form action="proses-edit.php" method="POST">  
     <fieldset>  
       <input type="hidden" name="id" value="<?php echo $prestasi['IDPRESTASI'] ?>" />  
     <p>  
       <label for="nama">Nomor Induk: </label>  
       <input type="number" name="noinduk" value="<?php echo $prestasi['NOINDUK'] ?>" />  
     </p>  
     <p>  
       <label for="alamat">Jenis Prestasi: </label>  
       <?php $jp = $prestasi['JENISPRESTASI'];?>  
         Akademik <input type="radio" name="jenis_prestasi" id="jenis_prestasi" value="0" <?php echo ($jp == 0) ? "checked": "" ?>>  
         Non-Akademik <input type="radio" name="jenis_prestasi" id="jenis_prestasi" value="1" <?php echo ($jp == 1) ? "checked": "" ?>>  
     </p>  
       <label for="nama">Keterangan: </label>  
       <input type="text" name="ket" value="<?php echo $prestasi['KETERANGAN'] ?>" />  
     <p>  
       <input type="submit" value="Simpan" name="simpan" />  
     </p>  
     </fieldset>  
   </form>  
   </body>  
 </html>  

proses-edit.php
 <?php  
 include("config.php");  
 // cek apakah tombol simpan sudah diklik atau blum?  
 if(isset($_POST['simpan'])){  
   // ambil data dari formulir  
   $id = $_POST['id'];  
   $induk = $_POST['noinduk'];  
   $jp = $_POST['jenis_prestasi'];  
   $ket = $_POST['ket'];  
   // buat query update  
   $sql = "UPDATE prestasi SET NOINDUK='$induk', JENISPRESTASI='$jp', KETERANGAN='$ket' WHERE IDPRESTASI=$id";  
   $query = mysqli_query($db, $sql);  
   // apakah query update berhasil?  
   if( $query ) {  
     // kalau berhasil alihkan ke halaman list-siswa.php  
     header('Location: list-prestasi.php');  
   } else {  
     // kalau gagal tampilkan pesan  
     die("Gagal menyimpan perubahan...");  
   }  
 } else {  
   die("Akses dilarang...");  
 }  
 ?>  

hapus.php
 <pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;background-image:URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEij6mb_g8eemHjS1NVAA4J5aq7Q_Oy4V_V6PAHDURQkzUaHNOrE8htoy__R8Ninaii-4JbWqbcGa_rU021T4YZKtOoBtUc-_ETRISNTyrmcPS1H0TQPvJZnT_mqeW1W7EEu3kP5RDtVsm1J/s320/codebg.gif);padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;"> &lt;?php   
  include("config.php");   
  if( isset($_GET['id']) ){   
   // ambil id dari query string   
   $id = $_GET['id'];   
   // buat query hapus   
   $sql = "DELETE FROM prestasi WHERE IDPRESTASI=$id";   
   $query = mysqli_query($db, $sql);   
   // apakah query hapus berhasil?   
   if( $query ){   
    header('Location: list-prestasi.php');   
   } else {   
    die("gagal menghapus...");   
   }   
  } else {   
   die("akses dilarang...");   
  }   
  ?&gt;   
 </code></pre>  

Berikut adalah hasilnya.

Halaman index.php

Saat List Prestasi diklik

Saat [+] Tambah Baru diklik


Saat tombol Tambah diklik, dengan form telah diisi

Saat tombol Edit diklik

Saat tombol Simpan di klik, dan Keterangannya di ubah

Saat Tombol Hapus diklik

Komentar