PWEB Tugas 9
Pada tugas ini diminta ditambahkan upload foto dari tugas 8 sebelumnya
berikut hasil website
Berikut codenya:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$server = "localhost"; | |
$user = "root"; | |
$password = ""; | |
$nama_database = "pendaftaran_siswa"; | |
$db = mysqli_connect($server, $user, $password, $nama_database); | |
if( !$db ){ | |
die("Gagal terhubung dengan database: " . mysqli_connect_error()); | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Form Pendaftaran Mahasiswa Baru</title> | |
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"> | |
</head> | |
<body class="d-flex h-100 text-white bg-secondary"> | |
<div class="container d-flex w-100 h-100 p-3 mx-auto flex-column"> | |
<header class="p-3 bg-secondary text-white mb-auto"> | |
<div class="container-fluid"> | |
<div class="d-flex flex-wrap align-items-center justify-content-center justify-content-lg-start"> | |
<ul class="nav col-12 col-lg-auto me-lg-auto mb-2 justify-content-center mb-md-0"> | |
<li><a href="./index.php" class="nav-link px-2 text-white">Home</a></li> | |
<li><a href="./form-daftar.php" class="nav-link px-2 text-black-50">Daftar</a></li> | |
<li><a href="./list-siswa.php" class="nav-link px-2 text-white">List Pendaftar</a></li> | |
</ul> | |
</div> | |
</div> | |
</header> | |
<main class="px-3 mx-auto" style="max-width:42em"> | |
<div class="container p-0 card shadow border-0 rounded" style="min-width: 40rem;"> | |
<div class="card-header bg-success text-center"> | |
<h3>Formulir Mahasiswa Baru</h3> | |
</div> | |
<form id="formMahasiswa" autocomplete="off" action="proses-pendaftaran.php" method="POST" class="bg-success text-white" enctype="multipart/form-data"> | |
<div class="form-group mt-3 mx-3"> | |
<label for="nama">Nama</label> | |
<input type="text" name="nama" placeholder="Nama Lengkap Calon Siswa" class="form-control" minlength="3" maxlength="40"> | |
</div> | |
<div class="form-group mt-3 mx-3"> | |
<label for="alamat">Alamat</label> | |
<textarea name="alamat" class="form-control" placeholder="Alamat Lengkap Siswa"></textarea> | |
</div> | |
<div class="form-group mt-3 mx-3"> | |
<label for="sekolah_asal">Sekolah Asal</label> | |
<input type="text" name="sekolah_asal" placeholder="Sekolah Asal Calon Siswa" class="form-control" minlength="1"> | |
</div> | |
<div class="form-group mt-3 mx-3"> | |
<label for="agama">Agama</label> | |
<select name="agama" class="form-control"> | |
<option>Pilih Agama Calon Siswa</option> | |
<option>Islam</option> | |
<option>Kristen</option> | |
<option>Hindu</option> | |
<option>Budha</option> | |
<option>Atheis</option> | |
</select> | |
</div> | |
<div class="form-group mt-3 mx-3"> | |
<label for="jenis_kelamin">Jenis Kelamin</label><br> | |
<input type="radio" name="jenis_kelamin" value="Laki-laki"> Laki-laki<br> | |
<input type="radio" name="jenis_kelamin" value="Perempuan"> Perempuan<br> | |
</div> | |
<div class="form-group mt-3 mx-3"> | |
<label for="foto">Foto</label><br><br> | |
<input type="file" name="foto" value=""><br><br> | |
<input type="checkbox" name="ubah_foto" value="true"> Ceklis jika ingin menambahkan foto<br> | |
</div> | |
<button name="daftar" type="submit" class="btn btn-primary form-group mt-3 mx-3 my-4" style="margin-top: 20px;">Daftar</button> | |
</form> | |
</div> | |
</main> | |
<footer class="mt-auto text-white-50 text-center"> | |
<p>powered by <a href="https://getbootstrap.com/" class="text-white">Bootstrap</a>.</p> | |
</footer> | |
</div> | |
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"> | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include("config.php"); | |
if (!isset($_GET['id'])) { | |
// kalau tidak ada id di query string | |
header('Location: list-siswa.php'); | |
} | |
//ambil id dari query string | |
$id = $_GET['id']; | |
// buat query untuk ambil data dari database | |
$sql = "SELECT * FROM calon_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 lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Form Pendaftaran Mahasiswa Baru</title> | |
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"> | |
</head> | |
<body class="d-flex h-100 text-white bg-secondary"> | |
<div class="container d-flex w-100 h-100 p-3 mx-auto flex-column"> | |
<header class="p-3 bg-secondary text-white mb-auto"> | |
<div class="container-fluid"> | |
<div class="d-flex flex-wrap align-items-center justify-content-center justify-content-lg-start"> | |
<ul class="nav col-12 col-lg-auto me-lg-auto mb-2 justify-content-center mb-md-0"> | |
<li><a href="./index.php" class="nav-link px-2 text-white">Home</a></li> | |
<li><a href="./form-daftar.php" class="nav-link px-2 text-white">Daftar</a></li> | |
<li><a href="./list-siswa.php" class="nav-link px-2 text-white">List Pendaftar</a></li> | |
</ul> | |
</div> | |
</div> | |
</header> | |
<main class="px-3 mx-auto" style="max-width:42em"> | |
<div class="container p-0 card shadow border-0 rounded" style="min-width: 40rem;"> | |
<div class="card-header bg-success text-center"> | |
<h3>Formulir Mahasiswa Baru</h3> | |
</div> | |
<form id="formMahasiswa" autocomplete="off" action="proses-edit.php" method="POST" class="bg-success text-white" enctype="multipart/form-data"> | |
<input type="hidden" name="id" value="<?php echo $siswa['id'] ?>"> | |
<div class="form-group mt-3 mx-3"> | |
<label for="nama">Nama</label> | |
<input type="text" name="nama" placeholder="Nama Lengkap Calon Siswa" class="form-control" minlength="3" maxlength="40" value="<?php echo $siswa['nama'] ?>"> | |
</div> | |
<div class="form-group mt-3 mx-3"> | |
<label for="alamat">Alamat</label> | |
<textarea name="alamat" class="form-control" placeholder="Alamat Lengkap Siswa"><?php echo $siswa['alamat'] ?></textarea> | |
</div> | |
<div class="form-group mt-3 mx-3"> | |
<label for="sekolah_asal">Sekolah Asal</label> | |
<input type="text" name="sekolah_asal" placeholder="Sekolah Asal Calon Siswa" class="form-control" minlength="1" value="<?php echo $siswa['sekolah_asal'] ?>"> | |
</div> | |
<div class="form-group mt-3 mx-3"> | |
<label for="agama">Agama</label> | |
<?php $agama = $siswa['agama']; ?> | |
<select name="agama" class="form-control"> | |
<option>Pilih Agama Calon Siswa</option> | |
<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> | |
</div> | |
<div class="form-group mt-3 mx-3"> | |
<label for="jenis_kelamin">Jenis Kelamin</label><br> | |
<?php $jk = $siswa['jenis_kelamin']; ?> | |
<input type="radio" name="jenis_kelamin" value="Laki-laki" <?php echo ($jk == 'Laki-laki') ? "checked" : "" ?>> Laki-laki<br> | |
<input type="radio" name="jenis_kelamin" value="Perempuan" <?php echo ($jk == 'Perempuan') ? "checked" : "" ?>> Perempuan<br> | |
</div> | |
<div class="form-group mt-3 mx-3"> | |
<label for="foto">Foto</label><br><br> | |
<input type="file" name="foto" value=""><br><br> | |
<input type="checkbox" name="ubah_foto" value="true"> Centang jika ingin mengubah foto<br> | |
</div> | |
<button name="simpan" type="submit" class="btn btn-primary form-group mt-3 mx-3 my-4" style="margin-top: 20px;">Simpan</button> | |
</form> | |
</div> | |
</main> | |
<footer class="mt-auto text-white-50 text-center"> | |
<p>powered by <a href="https://getbootstrap.com/" class="text-white">Bootstrap</a>.</p> | |
</footer> | |
</div> | |
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"> | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include("config.php"); | |
if( isset($_GET['id']) ){ | |
// ambil id dari query string | |
$id = $_GET['id']; | |
// buat query hapus | |
$sql = "DELETE FROM calon_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..."); | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Pendaftaran Mahasiswa Baru</title> | |
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"> | |
</head> | |
<body class="d-flex h-100 text-center text-white bg-success"> | |
<div class="container d-flex w-100 h-100 p-3 mx-auto flex-column"> | |
<header class="p-3 bg-success text-white mb-auto"> | |
<div class="container-fluid"> | |
<div class="d-flex flex-wrap align-items-center justify-content-center justify-content-lg-start"> | |
<ul class="nav col-12 col-lg-auto me-lg-auto mb-2 justify-content-center mb-md-0"> | |
<li><a href="./index.php" class="nav-link px-2 text-black-50">Home</a></li> | |
<li><a href="./form-daftar.php" class="nav-link px-2 text-white">Daftar</a></li> | |
<li><a href="./list-siswa.php" class="nav-link px-2 text-white">List Pendaftar</a></li> | |
</ul> | |
</div> | |
</div> | |
</header> | |
<main class="px-3 mx-auto bg-success" style="max-width:42em"> | |
<br> | |
<h1>Pendaftaran Mahasiswa Baru</h1> | |
<p class="lead"> | |
<a href="./form-daftar.php" class="btn btn-lg btn-secondary fw-bold border-white bg-white text-dark">Daftar Baru</a> | |
</p> | |
<p class="lead"> | |
<a href="./list-siswa.php" class="btn btn-lg btn-secondary fw-bold border-white bg-white text-dark">List Pendaftar</a> | |
</p> | |
</main> | |
<footer class="mt-auto text-white-50"> | |
<p>powered by <a href="https://getbootstrap.com/" class="text-white">Bootstrap</a>.</p> | |
</footer> | |
</div> | |
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"> | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php include("config.php"); ?> | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Pendaftaran Mahasiswa Baru</title> | |
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"> | |
</head> | |
<body class="d-flex h-100 text-white bg-secondary"> | |
<div class="container bg-success d-flex w-100 h-100 p-3 mx-auto flex-column"> | |
<header class="p-3 bg-success text-white mb-auto"> | |
<div class="container-fluid"> | |
<div class="d-flex flex-wrap align-items-center justify-content-center justify-content-lg-start"> | |
<ul class="nav col-12 col-lg-auto me-lg-auto mb-2 justify-content-center mb-md-0"> | |
<li><a href="./index.php" class="nav-link px-2 text-white">Home</a></li> | |
<li><a href="./form-daftar.php" class="nav-link px-2 text-white">Daftar</a></li> | |
<li><a href="./list-siswa.php" class="nav-link px-2 text-black-50">List Pendaftar</a></li> | |
</ul> | |
</div> | |
</div> | |
</header> | |
<main class="py-3 px-5"> | |
<h1>List Pendaftar</h1> | |
<p class="lead"> | |
<a href="./form-daftar.php" class="btn btn-sm btn-secondary fw-bold border-white bg-white text-dark">Tambah Baru</a> | |
</p> | |
<table class="table mt-4 bg-white"> | |
<thead> | |
<tr> | |
<th>ID</th> | |
<th>Nama</th> | |
<th>Alamat</th> | |
<th>Jenis Kelamin</th> | |
<th>Agama</th> | |
<th>Sekolah Asal</th> | |
<th>Foto</th> | |
<th>Tindakan</th> | |
</tr> | |
</thead> | |
<tbody> | |
<?php | |
$sql = "Select * From calon_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['sekolah_asal'] . "</td>"; | |
if($siswa['foto'] != NULL){ | |
echo "<td><img src='images/".$siswa['foto']."' width='100' height='100'></td>"; | |
} else{ | |
echo "<td><p>Tidak ada foto!</p></td>"; | |
} | |
echo "<td class='text-center'>"; | |
echo "<a class='btn btn-info btn-xs' href='form-edit.php?id=" . $siswa['id'] . "' ><span class='glyphicon glyphicon-edit'></span>Edit</a> | "; | |
echo "<a class='btn btn-danger btn-xs' href='hapus.php?id=" . $siswa['id'] . "'><span class='glyphicon glyphicon-remove'></span>Hapus</a>"; | |
echo "</td>"; | |
echo "</tr>"; | |
} | |
?> | |
</tbody> | |
</table> | |
<h5>Total: <?php echo mysqli_num_rows($query) ?></h5> | |
</main> | |
<footer class="mt-auto text-white-50 text-center"> | |
<p>powered by <a href="https://getbootstrap.com/" class="text-white">Bootstrap</a>.</p> | |
</footer> | |
</div> | |
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"> | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include("config.php"); | |
if (isset($_POST['simpan'])) { | |
$id = $_POST['id']; | |
$nama = $_POST['nama']; | |
$alamat = $_POST['alamat']; | |
$jk = $_POST['jenis_kelamin']; | |
$agama = $_POST['agama']; | |
$sekolah = $_POST['sekolah_asal']; | |
if(isset($_POST['ubah_foto'])){ | |
$foto = $_FILES['foto']['name']; | |
$tmp = $_FILES['foto']['tmp_name']; | |
$fotobaru = date('dmYHis').$foto; | |
$path = "images/".$fotobaru; | |
if(move_uploaded_file($tmp, $path)){ | |
$sql= "Select * From calon_siswa WHERE id='$id'"; | |
$query = mysqli_query($db, $sql); | |
$data = mysqli_fetch_array($query); | |
if(is_file("images/".$data['foto'])) | |
unlink("images/".$data['foto']); | |
$sql = "Update calon_siswa set nama='$nama', alamat='$alamat', jenis_kelamin='$jk', agama='$agama', sekolah_asal='$sekolah', foto='$fotobaru' Where id='$id'"; | |
$query = mysqli_query($db, $sql); | |
if ($query) { | |
header('Location: list-siswa.php'); | |
} else { | |
die("Gagal menyimpan perubahan..."); | |
} | |
} else{ | |
echo "alert(Maaf, Gambar gagal untuk diupload.)"; | |
header('Location: list-siswa.php'); | |
} | |
} | |
else { | |
$sql = "Update calon_siswa set nama='$nama', alamat='$alamat', jenis_kelamin='$jk', agama='$agama', sekolah_asal='$sekolah' Where id='$id'"; | |
$query = mysqli_query($db, $sql); | |
if ($query) { | |
header('Location: list-siswa.php'); | |
} else { | |
die("Gagal menyimpan perubahan..."); | |
} | |
} | |
} else{ | |
die('Akses Dilarang ...'); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include("config.php"); | |
if (isset($_POST['daftar'])) { | |
$nama = $_POST['nama']; | |
$alamat = $_POST['alamat']; | |
$jk = $_POST['jenis_kelamin']; | |
$agama = $_POST['agama']; | |
$sekolah = $_POST['sekolah_asal']; | |
if(isset($_POST['ubah_foto'])){ | |
$foto = $_FILES['foto']['name']; | |
$tmp = $_FILES['foto']['tmp_name']; | |
$fotobaru = date('dmYHis').$foto; | |
$path = "images/".$fotobaru; | |
if(move_uploaded_file($tmp, $path)){ | |
$sql = "Insert into calon_siswa (nama, alamat, jenis_kelamin, agama, sekolah_asal, foto) values ('$nama', '$alamat', '$jk', '$agama', '$sekolah', '$fotobaru')"; | |
$query = mysqli_query($db, $sql); | |
if ($query) { | |
header('Location: index.php?status=sukses'); | |
} else { | |
header('Location: index.php?status=gagal'); | |
} | |
} else{ | |
echo 'alert("Maaf, Gambar gagal untuk diupload")'; | |
echo "<br><a href='form-daftar.php'>Kembali Ke Form</a>"; | |
} | |
} | |
else{ | |
$sql = "Insert into calon_siswa (nama, alamat, jenis_kelamin, agama, sekolah_asal) values ('$nama', '$alamat', '$jk', '$agama', '$sekolah')"; | |
$query = mysqli_query($db, $sql); | |
if ($query) { | |
header('Location: index.php?status=sukses'); | |
} else { | |
header('Location: index.php?status=gagal'); | |
} | |
} | |
} | |
else{ | |
die("Akses dilarang..."); | |
} |
Comments
Post a Comment