Selasa, 16 Oktober 2018

UTS PWEB - Membuat Input Prestasi

Nama: Ayu Mutiara Sari
NRP: 05111740000149
Kelas: Pweb C

Hasil:





1. Database
2. Source Code 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());   
  }   
  ?>   

3. Source Code index.php

 <!DOCTYPE html>   
  <html>   
  <head>   
  <style type="text/css">   
  body   
  {   
   background:#FAEBD7;   
   font-family:courier;   
  }   
 </style>  
   <title>INPUT PRESTASI SISWA</title>   
  </head>   
  <body>   
   <header>   
 <div class= "kepala">    
    <h3>INPUT PRESTASI SISWA</h3>   
    <h3>SMA Coding</h3>   
 </div>  
   </header>   
   <h4>Menu</h4>   
   <nav>   
    <ul>   
     <li><a href="input_siswa.php">Input Prestasi</a></li>   
     <li><a href="list-siswa.php">List Prestasi Siswa</a></li>   
    </ul>   
   </nav>   
  <?php if(isset($_GET['status'])): ?>   
   <p>   
    <?php   
     if($_GET['status'] == 'sukses'){   
      echo "Pendaftaran siswa baru berhasil!";   
     } else {   
      echo "Pendaftaran gagal!";   
     }   
    ?>   
   </p>   
  <?php endif; ?>   
   </body>   
  </html>   

4. Source Code list-siswa.php

 <?php include("config.php"); ?>  
 <!DOCTYPE html>  
 <html>  
 <head>  
  <style type="text/css">   
  body   
  {   
   background:#FAEBD7;   
   font-family:courier;   
  }   
 </style>  
   <title>DAFTAR PRESTASI SISWA</title>  
 </head>  
 <body>  
   <header>  
     <h3>Berikut Ini Daftar Prestasi Siswa</h3>  
   </header>  
   <nav>  
     <a href="input_siswa.php">[+] Tambah Baru</a>  
   </nav>  
   <br>  
   <table border="1">  
   <thead>  
     <tr>  
       <th>No</th>  
       <th>Kelas</th>  
       <th>Semester</th>  
       <th>Nama Siswa</th>  
       <th>Prestasi</th>  
       <th>Keterangan</th>  
       <th>Tindakan</th>  
     </tr>  
   </thead>  
   <tbody>  
     <?php  
     $sql = "SELECT * FROM datasiswa";  
     $query = mysqli_query($db, $sql);  
     while($siswa = mysqli_fetch_array($query)){  
       echo "<tr>";  
       echo "<td>".$siswa['id']."</td>";  
       echo "<td>".$siswa['kelas']."</td>";  
       echo "<td>".$siswa['semester']."</td>";  
       echo "<td>".$siswa['nama']."</td>";  
       echo "<td>".$siswa['prestasi']."</td>";  
       echo "<td>".$siswa['keterangan']."</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>  

5. Source Code input-siswa.php

  <!DOCTYPE html>   
  <html>   
  <head>   
  <style type="text/css">   
  body   
  {   
   background:#FAEBD7;   
   font-family:courier;   
  }   
 </style>  
   <title>INPUT PRESTASI SISWA SMA CODING</title>   
  </head>   
  <body>   
   <header>   
    <h3>Input Prestasi Siswa</h3>   
   </header>   
   <form action="proses-input.php" method="POST">   
    <fieldset>   
    <p>   
     <label for="kelas">Kelas: </label>   
     <input type="text" name="kelas" placeholder="Kelas" />   
    </p>   
       <p>   
     <label for="semester">Semester: </label>   
     <input type="text" name="semester" placeholder="Semester" />   
    </p>  
      <p>   
     <label for="nama">Nama: </label>   
     <input type="text" name="nama" placeholder="Nama" />   
    </p>   
    <p>   
     <label for="prestasi">Prestasi: </label>   
     <textarea name="prestasi"></textarea>   
    </p>  
      <p>   
     <label for="keterangan">Keterangan: </label>   
     <textarea name="keterangan"></textarea>   
    </p>         
    <p>   
     <input type="submit" value="input" name="input" />   
    </p>   
    </fieldset>   
   </form>   
   </body>   
  </html>   

6. Source Code Input-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 datasiswa 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>  
  <style type="text/css">   
  body   
  {   
   background:#FAEBD7;   
   font-family:courier;   
  }   
 </style>   
   <title>Edit Input Prestasi</title>   
  </head>   
  <body>   
   <header>   
    <h3>Edit 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="kelas">Kelas: </label>   
     <input type="text" name="kelas" placeholder="kelas" value="<?php echo $siswa['kelas'] ?>" />   
    </p>         
    <p>   
     <label for="semester">Semester: </label>   
     <input type="text" name="semester" placeholder="semester" value="<?php echo $siswa['semester'] ?>" />   
    </p>  
       <p>   
     <label for="nama">Nama: </label>   
     <input type="text" name="nama" placeholder="nama lengkap" value="<?php echo $siswa['nama'] ?>" />   
    </p>  
    <p>   
     <label for="prestasi">Prestasi: </label>   
     <textarea name="prestasi"><?php echo $siswa['prestasi'] ?></textarea>   
    </p>   
      <p>   
     <label for="keterangan">Keterangan: </label>   
     <textarea name="keterangan"><?php echo $siswa['keterangan'] ?></textarea>   
    </p>        
    <p>   
     <input type="submit" value="Simpan" name="simpan" />   
    </p>   
    </fieldset>   
   </form>   
   </body>   
  </html>   

7. Source Code hapus.php
  <?php   
  include("config.php");   
  if( isset($_GET['id']) ){   
   // ambil id dari query string   
   $id = $_GET['id'];   
   // buat query hapus   
   $sql = "DELETE FROM datasiswa 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...");   
  }   
  ?>   

8. Source Code Proses-edit.php
 <?php   
  include("config.php");   
  
  if(isset($_POST['simpan'])){   
   $id = $_POST['id'];   
   $kelas = $_POST['kelas'];   
   $semester = $_POST['semester'];   
   $nama = $_POST['nama'];   
   $prestasi = $_POST['prestasi'];   
   $keterangan = $_POST['keterangan'];   
   // buat query update   
   $sql = "UPDATE datasiswa SET kelas='$kelas', semester='$semester', nama='$nama', prestasi='$prestasi', keterangan='$keterangan' 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...");   
  }   
  ?>   


9. Source Code proses_input.php


 <?php   
  include("config.php");   
    
  if(isset($_POST['input'])){    
   $kelas = $_POST['kelas'];   
   $semester = $_POST['semester'];   
   $nama = $_POST['nama'];   
   $prestasi = $_POST['prestasi'];   
   $keterangan = $_POST['keterangan'];   
   // buat query   
   $sql = "INSERT INTO datasiswa (kelas, semester, nama, prestasi, keterangan) VALUE ('$kelas', '$semester', '$nama', '$prestasi', '$keterangan')";   
   $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...");   
  }   
  ?>   

0 komentar:

Posting Komentar