UTS Pemrograman Web C - Manajemen Prestasi Siswa

Nama : Ifta Jihan Nabila
NRP : 05111740000034
Kelas : PWEB C

UTS kali ini membuat database yang menyimpan data siswa beserta jenis prestasi dan nilainya.

Source code:
1. 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());  
 }  
 ?>  


2. index.php

 <!DOCTYPE html>  
 <html>  
 <head>  
   <title>SMAN 352 Surabaya</title>  
 </head>  
 <style type="text/css">  
   body{  
     background-color: antiquewhite;  
   }  
 </style>  
 <body style="text-align: center;">  
   <header>  
     <h3>Input Data Prestasi Siswa</h3>  
     <h1>SMAN 352 Surabaya</h1>  
   </header>  
   <h4>Menu</h4>  
   <nav>  
       <a href="form-daftar.php">Input Data Siswa</a> <br>  
       <a href="list-siswa.php">Daftar Prestasi Siswa</a>  
   </nav>  
 <?php if(isset($_GET['status'])): ?>  
   <p>  
     <?php  
       if($_GET['status'] == 'sukses'){  
         echo "Pendaftaran mahasiswa baru berhasil!";  
       } else {  
         echo "Pendaftaran gagal!";  
       }  
     ?>  
   </p>  
 <?php endif; ?>  
   </body>  
 </html>  


3. form-daftar.php

 <!DOCTYPE html>  
 <html>  
 <head>  
   <title>SMAN 352 Surabaya</title>  
 </head>  
 <style type="text/css">  
   body{  
     background-color: antiquewhite;  
   }  
 </style>  
 <body style="text-align: center;">  
   <header>  
     <h3>Input Data Prestasi Siswa</h3>  
   </header>  
   <form action="proses-pendaftaran.php" method="POST">  
     <fieldset>  
     <p>  
       <label for="id">Id: </label>  
       <input type="number" name="id">  
     </p>  
     <p>  
       <label for="nama">Nama: </label>  
       <input type="text" name="nama" placeholder="nama lengkap" />  
     </p>  
     <p>  
       <label for="alamat">Alamat: </label>  
       <textarea name="alamat"></textarea>  
     </p>  
     <p>  
       <label for="jenis_kelamin">Jenis Kelamin: </label>  
       <label><input type="radio" name="jenis_kelamin" value="laki-laki"> Laki-laki</label>  
       <label><input type="radio" name="jenis_kelamin" value="perempuan"> Perempuan</label>  
     </p>  
     <p>  
       <label for="agama">Agama: </label>  
       <select name="agama">  
         <option>Islam</option>  
         <option>Kristen</option>  
         <option>Hindu</option>  
         <option>Budha</option>  
         <option>Atheis</option>  
       </select>  
     </p>  
     <p>  
       <label for="kelas">Kelas: </label>  
       <input type="text" name="kelas" placeholder="kelas" />  
     </p>  
      <p>  
       <label for="ijin">Total Ijin: </label>  
       <input type="number" name="ijin" placeholder="Total ijin" />  
     </p>  
      <p>  
       <label for="absen">Total Absen: </label>  
       <input type="number" name="absen" placeholder="Total Absen" />  
     </p>  
      <p>  
       <label for="sakit">Total Sakit: </label>  
       <input type="number" name="sakit" placeholder="Total Sakit" />  
     </p>  
      <p>  
       <label for="jenis_prestasi">Jenis Prestasi: </label>  
       <input type="text" name="jenis_prestasi" placeholder="Prestasi" />  
     </p>  
      <p>  
       <label for="keterangan">Keterangan: </label>  
       <input type="text" name="keterangan" placeholder="keterangan" />  
     </p>  
      <p>  
       <label for="nilai">Nilai: </label>  
       <input type="text" name="nilai" placeholder="Nilai" />  
     </p>  
     <p>  
       <input type="submit" value="Daftar" name="daftar" />  
     </p>  
     </fieldset>  
   </form>  
   </body>  
 </html>  


4. proses-pendaftaran.php

 <?php  
 include("config.php");  
 // cek apakah tombol daftar sudah diklik atau blum?  
 if(isset($_POST['daftar'])){  
   // ambil data dari formulir  
   $id = $_POST['id'];  
   $nama = $_POST['nama'];  
   $alamat = $_POST['alamat'];  
   $jk = $_POST['jenis_kelamin'];  
   $agama = $_POST['agama'];  
   $kelas = $_POST['kelas'];  
   $ijin = $_POST['ijin'];  
   $absen = $_POST['absen'];  
   $sakit = $_POST['sakit'];  
   $jenis_prestasi = $_POST['jenis_prestasi'];  
   $keterangan = $_POST['keterangan'];  
   $nilai = $_POST['nilai'];  
   // buat query  
   $sql = "INSERT INTO siswa (nama, alamat, jenis_kelamin, agama, kelas, ijin, absen, sakit, jenis_prestasi, keterangan, nilai) VALUE ('$nama', '$alamat', '$jk', '$agama', '$kelas', '$ijin', '$absen', '$sakit', '$jenis_prestasi', '$keterangan', '$nilai')";  
   $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...");  
 }  
 ?>  


5. list-siswa.php

 <?php include("config.php"); ?>  
 <!DOCTYPE html>  
 <html>  
 <head>  
   <title>SMAN 352 Surabaya</title>  
 </head>  
 <style type="text/css">  
   body{  
     background-color: antiquewhite;  
   }  
 </style>  
 <body style="text-align: center;">  
   <header>  
     <h3>List Siswa SMAN 352 Surabaya</h3>  
   </header>  
   <nav>  
     <a href="form-daftar.php">[+] Tambah Baru</a>  
   </nav>  
   <br>  
   <table border="1">  
   <thead>  
     <tr>  
       <th>No</th>  
       <th>Nama</th>  
       <th>Alamat</th>  
       <th>Jenis Kelamin</th>  
       <th>Agama</th>  
       <th>Kelas</th>  
       <th>Total Ijin</th>  
       <th>Total Absen</th>  
       <th>Total Sakit</th>  
       <th>Jenis Prestasi</th>  
       <th>Keterangan</th>  
       <th>Nilai</th>  
       <th>Edit/Hapus</th>  
     </tr>  
   </thead>  
   <tbody>  
     <?php  
     $sql = "SELECT * FROM siswa";  
     $query = mysqli_query($db, $sql);  
     while($siswa = mysqli_fetch_array($query)){  
       echo "<tr>";  
       echo "<td>".$siswa['id']."</td>";  
       echo "<td>".$siswa['nama']."</td>";  
       echo "<td>".$siswa['alamat']."</td>";  
       echo "<td>".$siswa['jenis_kelamin']."</td>";  
       echo "<td>".$siswa['agama']."</td>";  
       echo "<td>".$siswa['kelas']."</td>";  
       echo "<td>".$siswa['ijin']."</td>";  
       echo "<td>".$siswa['absen']."</td>";  
       echo "<td>".$siswa['sakit']."</td>";  
       echo "<td>".$siswa['jenis_prestasi']."</td>";  
       echo "<td>".$siswa['keterangan']."</td>";  
       echo "<td>".$siswa['nilai']."</td>";  
       echo "<td>";  
       echo "<a href='form-edit.php?id=".$siswa['id']."'>Edit</a> | ";  
       echo "<a href='hapus.php?id=".$siswa['id']."'>Hapus</a>";  
       echo "</td>";  
       echo "</tr>";  
     }  
     ?>  
   </tbody>  
   </table>  
   <p>Total: <?php echo mysqli_num_rows($query) ?></p>  
   </body>  
 </html>  

6. form-edit.php

 <?php  
 include("config.php");  
 // kalau tidak ada id di query string  
 if( !isset($_GET['id']) ){  
   header('Location: list-siswa.php');  
 }  
 //ambil id dari query string  
 $id = $_GET['id'];  
 // buat query untuk ambil data dari database  
 $sql = "SELECT * FROM siswa WHERE id=$id";  
 $query = mysqli_query($db, $sql);  
 $siswa = 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>SMAN 352 Surabaya</title>  
 </head>  
 <style type="text/css">  
   body{  
     background-color: antiquewhite;  
   }  
 </style>  
 <body style="text-align: center;">  
   <header>  
     <h3>Edit Data Siswa</h3>  
   </header>  
   <form action="proses-edit.php" method="POST">  
     <fieldset>  
       <input type="hidden" name="id" value="<?php echo $siswa['id'] ?>" />  
     <p>  
       <label for="nama">Nama: </label>  
       <input type="text" name="nama" placeholder="nama lengkap" value="<?php echo $siswa['nama'] ?>" />  
     </p>  
     <p>  
       <label for="alamat">Alamat: </label>  
       <textarea name="alamat"><?php echo $siswa['alamat'] ?></textarea>  
     </p>  
     <p>  
       <label for="jenis_kelamin">Jenis Kelamin: </label>  
       <?php $jk = $siswa['jenis_kelamin']; ?>  
       <label><input type="radio" name="jenis_kelamin" value="laki-laki" <?php echo ($jk == 'laki-laki') ? "checked": "" ?>> Laki-laki</label>  
       <label><input type="radio" name="jenis_kelamin" value="perempuan" <?php echo ($jk == 'perempuan') ? "checked": "" ?>> Perempuan</label>  
     </p>  
     <p>  
       <label for="agama">Agama: </label>  
       <?php $agama = $siswa['agama']; ?>  
       <select name="agama">  
         <option <?php echo ($agama == 'Islam') ? "selected": "" ?>>Islam</option>  
         <option <?php echo ($agama == 'Kristen') ? "selected": "" ?>>Kristen</option>  
         <option <?php echo ($agama == 'Hindu') ? "selected": "" ?>>Hindu</option>  
         <option <?php echo ($agama == 'Budha') ? "selected": "" ?>>Budha</option>  
         <option <?php echo ($agama == 'Atheis') ? "selected": "" ?>>Atheis</option>  
       </select>  
     </p>  
     <p>  
       <label for="kelas">Kelas: </label>  
       <input type="text" name="kelas" placeholder="kelas" value="<?php echo $siswa['kelas'] ?>" />  
     </p>  
     <p>  
       <label for="ijin">Total Ijin: </label>  
       <input type="number" name="ijin" placeholder="Total ijin" value="<?php echo $siswa['ijin'] ?>"/>  
     </p>  
      <p>  
       <label for="absen">Total Absen: </label>  
       <input type="number" name="absen" placeholder="Total Absen" value="<?php echo $siswa['absen'] ?>"/>  
     </p>  
      <p>  
       <label for="sakit">Total Sakit: </label>  
       <input type="number" name="sakit" placeholder="Total Sakit" value="<?php echo $siswa['sakit'] ?>"/>  
     </p>  
      <p>  
       <label for="jenis_prestasi">Jenis Prestasi: </label>  
       <input type="text" name="jenis_prestasi" placeholder="Prestasi" value="<?php echo $siswa['jenis_prestasi'] ?>"/>  
     </p>  
      <p>  
       <label for="keterangan">Keterangan: </label>  
       <input type="text" name="keterangan" placeholder="keterangan" value="<?php echo $siswa['keterangan'] ?>"/>  
     </p>  
      <p>  
       <label for="nilai">Nilai: </label>  
       <input type="text" name="nilai" placeholder="Nilai" value="<?php echo $siswa['nilai'] ?>"/>  
     </p>  
     <p>  
       <input type="submit" value="Simpan" name="simpan" />  
     </p>  
     </fieldset>  
   </form>  
   </body>  
 </html>  

7. 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'];  
   $nama = $_POST['nama'];  
   $alamat = $_POST['alamat'];  
   $jk = $_POST['jenis_kelamin'];  
   $agama = $_POST['agama'];  
   $kelas = $_POST['kelas'];  
   $ijin = $_POST['ijin'];  
   $absen = $_POST['absen'];  
   $sakit = $_POST['sakit'];  
   $jenis_prestasi = $_POST['jenis_prestasi'];  
   $keterangan = $_POST['keterangan'];  
   $nilai = $_POST['nilai'];  
   // buat query update  
   $sql = "UPDATE siswa SET nama='$nama', alamat='$alamat', jenis_kelamin='$jk', agama='$agama', kelas='$kelas', ijin='$ijin', absen='$absen', sakit='$sakit', jenis_prestasi='$jenis_prestasi', keterangan='$keterangan', nilai='$nilai' WHERE id=$id";  
   $query = mysqli_query($db, $sql);  
   // apakah query update berhasil?  
   if( $query ) {  
     // kalau berhasil alihkan ke halaman list-siswa.php  
     header('Location: list-siswa.php');  
   } else {  
     // kalau gagal tampilkan pesan  
     die("Gagal menyimpan perubahan...");  
   }  
 } else {  
   die("Akses dilarang...");  
 }  
 ?>  

8. hapus.php

 <?php  
 include("config.php");  
 if( isset($_GET['id']) ){  
   // ambil id dari query string  
   $id = $_GET['id'];  
   // buat query hapus  
   $sql = "DELETE FROM siswa WHERE id=$id";  
   $query = mysqli_query($db, $sql);  
   // apakah query hapus berhasil?  
   if( $query ){  
     header('Location: list-siswa.php');  
   } else {  
     die("gagal menghapus...");  
   }  
 } else {  
   die("akses dilarang...");  
 }  
 ?>  


Hasil output:

Tampilan Awal 

Tampilan List Siswa

Tampilan Input Siswa

Tampilan Setelah Input Siswa

Tampilan Setelah edit nilai siswa

Tampilan hapus data siswa

Database 


Terimakasih.

Comments

Popular Posts