Selasa, 16 Oktober 2018

PWEB C- UTS

Pada kesempatan kali ini saya ditugaskan membuat web untuk menginput daftar prestasi siswa dengan php dan mysql. Berikut adalah hasilnya:
1.home
2. formulir
3. list prestasi

Berikut saya lampirkan pula ´source codenya:
1. config.php

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

<!DOCTYPE html>  
 <html>  
 <head>  
   <title>Formulir Prestasi Siswa Kelas 9A | SMA SKJ</title>  
 </head>  
 <body>  
   <header>  
     <h3>Formulir Prestasi Siswa Kelas 9A</h3>  
   </header>  
   <form action="proses_daftar.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" value="<?php echo $siswa['Nama'] ?>" />  
     </p>  
     <p>  
       <label for="NIS">NIS: </label>  
       <input type="text" name="NIS" value="<?php echo $siswa['NIS'] ?>" />  
     </p>  
     <p>  
       <label for="Bidang">Bidang Prestasi: </label>
        <input type="text" name="Bidang" value="<?php echo $siswa['Bidang'] ?>" />       
       </p>  
     <p>  
       <label for="Prestasi">Prestasi: </label>  
       <textarea name="Prestasi"><?php echo $siswa['Prestasi'] ?></textarea>  
     </p>  
     <p>  
       <input type="submit" value="Simpan" name="simpan" />  
     </p>  
     </fieldset>  
   </form>  
   </body>  
 </html>  
3. form_edit.php

<?php  
 include("config.php");   
 if( !isset($_GET['id']) ){  
   header('Location: list_siswa.php');  
 }  
 $id = $_GET['id'];  
 $sql = "SELECT * FROM prestasi WHERE id=$id";  
 $query = mysqli_query($db, $sql);  
 $siswa = mysqli_fetch_assoc($query);  
 if( mysqli_num_rows($query) < 1 ){  
   die("data tidak ditemukan...");  
 }  
 ?>  
 <!DOCTYPE html>  
 <html>  
 <head>  
   <title>Formulir Perubahan Prestasi Siswa Kelas 9A | SMA SKJ</title>  
 </head>  
 <body>  
   <header>  
     <h3>Formulir Perubahan Prestasi 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="NIS">NIS: </label>  
       <textarea name="NIS"><?php echo $siswa['nis'] ?></textarea>  
     </p>  
     <p>  
       <label for="Bidang">Bidang Prestasi: </label>  
       <?php $jk = $siswa['Bidang']; ?>  
       <label><input type="radio" name="Bidang" value="Akademik" <?php echo ($Bidang == 'Akademik') ? "checked": "" ?>> Akademik</label>  
       <label><input type="radio" name="Bidang" value="non-Akademik" <?php echo ($Bidang == 'non-Akademik') ? "checked": "" ?>> non-Akademik</label>  
     </p>  
     <p>  
       <label for="Prestasi">Prestasi: </label>  
       <input type="text" name="Prestasi" placeholder="Prestasi" value="<?php echo $siswa['Prestasi'] ?>" />  
     </p>  
     <p>  
       <input type="submit" value="Simpan" name="simpan" />  
     </p>  
     </fieldset>  
   </form>  
   </body>  
 </html>  
4. hapus.php

<?php  
 include("config.php");  
 if( isset($_GET['id']) ){  
   $id = $_GET['id'];  
   $sql = "DELETE FROM prestasi WHERE id=$id";  
   $query = mysqli_query($db, $sql);  
   if( $query ){  
     header('Location: list_siswa.php');  
   } else {  
     die("gagal menghapus...");  
   }  
 } else {  
   die("akses dilarang...");  
 }  
 ?>  
5. index.php

<!DOCTYPE html>  
 <html>  
 <head>  
   <title>Data Siswa Berprestasi SMA SKJ</title>  
 </head>  
 <body>  
   <header>  
     <h3>Daftar Siswa Berprestasi</h3>  
     <h1>Kelas 9A</h1>  
   </header>  
   <h4>Menu</h4>  
   <nav>  
     <ul>  
       <li><a href="form_daftar.php">Input Prestasi Siswa</a></li>  
       <li><a href="list_siswa.php">List Prestasi</a></li>  
     </ul>  
   </nav>  
 <?php if(isset($_GET['status'])): ?>  
   <p>  
     <?php  
       if($_GET['status'] == 'sukses'){  
         echo "Input prestasi siswa berhasil!";  
       } else {  
         echo "Penginputan gagal!";  
       }  
     ?>  
   </p>  
 <?php endif; ?>  
   </body>  
 </html>  
6. list_siswa.php

<?php include("config.php"); ?>  
 <!DOCTYPE html>  
 <html>  
 <head>  
   <title>Pendaftaran Siswa Baru | SMK Coding</title>  
 </head>  
 <body>  
   <header>  
     <h3>Siswa yang berprestasi</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>NIS</th>  
       <th>Bidang Prestasi</th>  
       <th>Prestasu</th>  
       <th>Tindakan</th>  
     </tr>  
   </thead>  
   <tbody>  
     <?php  
     $sql = "SELECT * FROM prestasi";  
     $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['NIS']."</td>";  
       echo "<td>".$siswa['Bidang']."</td>";  
       echo "<td>".$siswa['Prestasi']."</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>  
7. proses_daftar.php

 <?php  
 include("config.php");  
 if(isset($_POST['simpan'])){  
  $id = $_POST['id'];  
   $Nama = $_POST['Nama'];  
   $NIS = $_POST['NIS'];  
   $Bidang = $_POST['Bidang'];  
   $Prestasi = $_POST['Prestasi'];  
   $sql = "UPDATE prestasi nama='$Nama', NIS='$NIS', Bidang='$Bidang', Prestasi='$Prestasi', WHERE id=$id";  
   $query = mysqli_query($db, $sql);  
   if( $query ) {  
     header('Location: index.php?status=sukses');  
   } else {  
     header('Location: index.php?status=gagal');  
   }  
 } else {  
   die("Akses dilarang...");  
 }  
 ?>  
8. proses_edit.php

<?php  
 include("config.php");  
 if(isset($_POST['simpan'])){  
   $id = $_POST['id'];  
   $Nama = $_POST['Nama'];  
   $NIS = $_POST['NIS'];  
   $Bidang = $_POST['Bidang'];  
   $Prestasi = $_POST['Prestasi'];  
   $sql = "UPDATE prestasi nama='$Nama', NIS='$NIS', Bidang='$Bidang', Prestasi='$Prestasi', WHERE id=$id";  
   $query = mysqli_query($db, $sql); 
   if( $query ) {  
     header('Location: list siswa.php');  
   } else {  
     die("Gagal menyimpan perubahan...");  
   }  
 } else {  
   die("Akses dilarang...");  
 }  
 ?>  

Tidak ada komentar:

Posting Komentar