UTS PWEB C
1. tampilan index
Source code :
1. config.php
2. form-daftar.php
3. form-edit.php
4. hapus.php
5. index.php
6. list-siswa.php
7. proses-edit.php
8. proses-pendaftaran.php
2. menambahkan prestasi
3. list prestasi yang terdaftar
4. mengedit prestasi
5. setelah di edit
Source code :
1. config.php
<?php
$server = "localhost";
$user = "root";
$password = "";
$nama_database = "siswa";
$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>Sistem Akademik</title>
</head>
<body>
<header>
<h3>Formulir Input Prestasi</h3>
</header>
<form action="proses-pendaftaran.php" method="POST">
<fieldset>
<p>
<label for="nama">Nama: </label>
<input type="text" name="nama" placeholder="nama lengkap" />
</p>
<p>
<label for="kelas">kelas: </label>
<input type="text" name="kelas" placeholder="kelas" />
</p>
<p>
<label for="kelas">Prestasi yang didapat: </label>
<textarea name="prestasi"></textarea>
</p>
<p>
<input type="submit" value="Daftar" name="daftar" />
</p>
</fieldset>
</form>
</body>
</html>
3. 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 kelas 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>Sistem akademik</title>
</head>
<body>
<header>
<h3>Formulir Edit</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="kelas">Kelas: </label>
<input type="text" name="kelas" placeholder="kelas" value="<?php echo $siswa['kelas'] ?>" />
</p>
<p>
<label for="kelas">Prestasi: </label>
<textarea name="prestasi"><?php echo $siswa['prestasi'] ?></textarea>
</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']) ){
// ambil id dari query string
$id = $_GET['id'];
// buat query hapus
$sql = "DELETE FROM kelas WHERE id=$id";
$query = mysqli_query($db, $sql);
// apakah query hapus berhasil?
if( $query ){
$id = 0;
header('Location: list-siswa.php');
} else {
die("gagal menghapus...");
}
} else {
die("akses dilarang...");
}
?>
5. index.php
<!DOCTYPE html>
<html>
<head>
<title>Sistem Akademik</title>
</head>
<body>
<header>
<h3>SMA Coding</h3>
<h1>E - Rapor</h1>
</header>
<h4>Menu</h4>
<nav>
<ul>
<li><a href="form-daftar.php">Tambah Prestasi Baru</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 "Berhasil";
} else {
echo "Gagal!";
}
?>
</p>
<?php endif; ?>
</body>
</html>
6. list-siswa.php
<?php include("config.php"); ?>
<!DOCTYPE html>
<html>
<head>
<title>Sistem Akademik</title>
</head>
<body>
<header>
<h3>Prestasi yang sudah terdaftar</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>Kelas</th>
<th>Prestasi</th>
<th>Tindakan</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT * FROM kelas";
$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['kelas']."</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-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'];
$kelas = $_POST['kelas'];
$prestasi = $_POST['prestasi'];
// buat query update
$sql = "UPDATE kelas SET nama='$nama', kelas='$kelas', prestasi='$prestasi' 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. proses-pendaftaran.php
<?php
include("config.php");
if(isset($_POST['daftar'])){
$name = $_POST['nama'];
$kelas = $_POST['kelas'];
$prestasi = $_POST['prestasi'];
$sql = "INSERT INTO kelas (nama, kelas, prestasi) VALUE ('$name', '$kelas', '$prestasi')";
$query = mysqli_query($db, $sql);
if( $query ) {
echo "Sukses!!";
header('Location: index.php?status=sukses');
} else {
echo "Gagal!!";
header('Location: index.php?status=gagal');
}
} else {
die("Akses dilarang...");
}
?>
Komentar
Posting Komentar