Tugas WebDinamis2

Tugas kali ini membahas suatu studi kasus sesuai dengan ketentuan yang diberikan dalam tugas.
Langkah pertama adalah pembuatan database beserta dengan tabel. Melalui phpmyadmin kita buat database dengan nama tugas. Selanjtnya membuat tabel, disini kita akan membuat empat buah tabel terkait yaitu tabel mahasiswa, dosen, matakuliah, dan tabel pengampu matkul.

Pertama buat tabel mahasiswa dengan struktur sebagai berikut:
Field Tipe Data
nim int(8)
nama varchar(30)
email varchar(25)
kode_mk int(8)
id_dosen int(8)

Tabel Dosen:
Field Tipe Data
id_dosen int(8)
nama_dosen varchar(30)
email varchar(25)

Tabel Matakuliah:
Field Tipe Data
kode_mk varchar(8)
nama_mk varchar(30)
bobot varchar(2)
id_dosen int(8)

Tabel Pengampu_matkul:
Field Tipe Data
id_dosen int(8)
kode_mk int(8)

Selanjutnya kita buat file tugas.php dengan menggunakan trigger(parameter), isi script adalah sebagai berikut:

<?php
$koneksi=mysql_connect('localhost','root','')or die('Koneksi Gagal');
if(! $koneksi) {
echo"Gagal koneksi boss...!";
}
mysql_select_db('tugas')or die('Database tidak di temukan');
echo('<hr align="center" size="2" width="auto" noshade>');
echo('<table>');
echo('<tr><td><b>Menu Utama : </b></td><td><a href="tugas.php?input=1"><b>Input Data</b></a></td>');
echo('<td>&nbsp;</td><td><a href="tugas.php?tampil=1"><b>&nbsp;Tampil Data</b></a></td></tr>');
echo('</table>');
echo('<p>');
echo('<hr align="center" size="2" width="auto" noshade>');
echo('</p>');

if($_GET['input']=='1') {
echo('<table>');
echo('<tr><td><a href="tugas.php?mhs=1">Input Data Mahasiswa</a></td></tr>');
echo('<tr><td><a href="tugas.php?dosen=1">Input Data Dosen</a></td></tr>');
echo('<tr><td><a href="tugas.php?matkul=1">Input Data Matakuliah</a></td></tr>');
echo('<tr><td><a href="tugas.php?pengampu_mk=1">Input Data Pengampu Matakuliah</a></td></tr>');
echo('</table>');
}

if($_GET['tampil']=='1') {
echo('<table>');
echo('<tr><td><a href="tugas.php?tampilmhs=1">Tampil Data Mahasiswa</a></td></tr>');
echo('<tr><td><a href="tugas.php?tampilmatkul=1">Tampil Data Matakuliah</a></td></tr>');
echo('</table>');
}

if ($_GET['mhs'] == '1') {
echo('<form name="form1" method="GET" action="tugas.php?TbSimpan1">');
echo('<table width="450" border="0" cellpadding="3" cellspacing="0" class="keliling">');
echo('<tr><td colspan="3"><b>MASUKKAN DATAA MAHASISWA</b></td></tr>');
echo('<tr><td>NIM</td><td><input name="txtnim" type="text" maxlength="8" value=""></td></tr>');
echo('<tr><td>Nama</td><td><input name="txtnama" type="text" maxlength="30" value=""></td></tr>');
echo('<tr><td>Email</td><td><input name="txtemail" type="text" maxlength="25" value=""></td></tr>');
echo('<tr><td>Kode MatKul</td><td><input name="txtkodemk" type="text" maxlength="25" value=""></td></tr>');
echo('<tr><td>ID Dosen Wali</td><td><input name="txtidwali" type="text" maxlength="25" value=""></td></tr>');
echo('<tr><td>&nbsp;</td><td><input name="TbSimpan1" type="submit" value="Simpan"></td></tr>');
echo('</table></form>');
}

if ($_GET['dosen'] == '1'){
echo('<form name="form2" method="GET" action="tugas.php?TbSimpan2">');
echo('<table width="450" border="0" cellpadding="3" cellspacing="0" class="keliling">');
echo('<tr><td colspan="3"><b>MASUKKAN DATA DOSEN</b></td></tr>');
echo('<tr><td>ID</td><td><input name="txtiddosen" type="text" maxlength="8" value=""></td></tr>');
echo('<tr><td>Nama</td><td><input name="txtnmdosen" type="text" maxlength="30" value=""></td></tr>');
echo('<tr><td>Email</td><td><input name="txtemaildosen" type="text" maxlength="25" value=""></td></tr>');
echo('<tr><td>&nbsp;</td><td><input name="TbSimpan2" type="submit" value="Simpan"></td></tr>');
echo('</table></form>');
}

if ($_GET['matkul'] == '1'){
echo('<form name="form2" method="GET" action="tugas.php?TbSimpan3">');
echo('<table width="450" border="0" cellpadding="3" cellspacing="0" class="keliling">');
echo('<tr><td colspan="3"><b>MASUKKAN DATA MATAKULIAH</b></td></tr>');
echo('<tr><td>Kode MK</td><td><input name="txtkodemk" type="text" maxlength="8" value=""></td></tr>');
echo('<tr><td>Nama MK</td><td><input name="txtnmmk" type="text" maxlength="30" value=""></td></tr>');
echo('<tr><td>Bobot</td><td><input name="txtbobot" type="text" maxlength="25" value=""></td></tr>');
echo('<tr><td>Id Dosen</td><td><input name="txtiddosen" type="text" maxlength="25" value=""></td></tr>');
echo('<tr><td>&nbsp;</td><td><input name="TbSimpan3" type="submit" value="Simpan"></td></tr>');
echo('</table></form>');
}

if ($_GET['pengampu_mk'] == '1'){
echo('<form name="form2" method="GET" action="tugas.php?TbSimpan4">');
echo('<table width="450" border="0" cellpadding="3" cellspacing="0" class="keliling">');
echo('<tr><td colspan="3"><b>MASUKKAN DATA MATAKULIAH</b></td></tr>');
echo('<tr><td>Kode MK</td><td><input name="txtkodemk" type="text" maxlength="8" value=""></td></tr>');
echo('<tr><td>ID Dosen</td><td><input name="txtiddosen" type="text" maxlength="30" value=""></td></tr>');
echo('<tr><td>&nbsp;</td><td><input name="TbSimpan4" type="submit" value="Simpan"></td></tr>');
echo('</table></form>');
}

if ($_GET['TbSimpan1']) {
$txtnim=$_GET['txtnim'];
$txtnama=$_GET['txtnama'];
$txtemail=$_GET['txtemail'];
$txtkodemk=$_GET['txtkodemk'];
$txtidwali=$_GET['txtidwali'];

if(trim($txtnim)=="") {
$pesan[]="Data NIM kosong";
}

if(! count($pesan)==0) {
$Nim=$_GET['txtnim'];
$Nama=$_GET['txtnama'];
$Email=$_GET['txtemail'];
$Kodemk=$_GET['txtkodemk'];
$Idwali=$_GET['txtidwali'];

}

else {
$sql="insert into mahasiswa(nim,nama,email,kode_mk,id_dosen) values('$txtnim','$txtnama','$txtemail','$txtkodemk','$txtidwali')";

mysql_query($sql,$koneksi) or die ("Gagal kuery Simpan mhs".mysql_error());
echo"Proses Simpan Berhasil";
}
}


if ($_GET['TbSimpan2']) {
$txtiddosen=$_GET['txtiddosen'];
$txtnmdosen=$_GET['txtnmdosen'];
$txtemaildosen=$_GET['txtemaildosen'];

if(trim($txtiddosen)=="") {
$pesan[]="Data ID kosong";
}

if(! count($pesan)==0) {
$Id=$_GET['txtiddosen'];
$Nama=$_GET['txtnmdosen'];
$Email=$_GET['txtemaildosen'];
}

else {
$sql="insert into dosen(id_dosen,nama_dosen,email) values('$txtiddosen','$txtnmdosen','$txtemaildosen')";

mysql_query($sql,$koneksi) or die ("Gagal kuery Simpan Dosen".mysql_error());
echo"Proses Simpan Berhasil";
}
}

if ($_GET['TbSimpan3']) {
$txtkodemk=$_GET['txtkodemk'];
$txtnmmk=$_GET['txtnmmk'];
$txtbobot=$_GET['txtbobot'];
$txtiddosen=$_GET['txtiddosen'];

if(trim($txtkodemk)=="") {
$pesan[]="Data ID kosong";
}

if(! count($pesan)==0) {
$Kode=$_GET['txtkodemk'];
$Nama=$_GET['txtnmmk'];
$Bobot=$_GET['txtbobot'];
$IdDosen=$_GET['txtiddosen'];
}

else {
$sql="insert into matakuliah(kode_mk,nama_mk,bobot,id_dosen) values('$txtkodemk','$txtnmmk','$txtbobot','$txtiddosen')";

mysql_query($sql,$koneksi) or die ("Gagal kuery Simpan Dosen".mysql_error());
echo"Proses Simpan Berhasil";
}
}

if ($_GET['TbSimpan4']) {
$txtkodemk=$_GET['txtkodemk'];
$txtiddosen=$_GET['txtiddosen'];

if(trim($txtkodemk)=="") {
$pesan[]="Data kode mk kosong";
}

if(! count($pesan)==0) {
$Kode=$_GET['txtkodemk'];
$IDdosen=$_GET['txtiddosen'];
}

else {
$sql="insert into pengampu_matkul(kode_mk,id_dosen) values('$txtkodemk','$txtiddosen')";

mysql_query($sql,$koneksi) or die ("Gagal kuery Simpan Dosen".mysql_error());
echo"Proses Simpan Berhasil";
}
}

if($_GET['tampilmhs']=='1') {
$sql = " SELECT mahasiswa.nim,mahasiswa.nama,dosen.nama_dosen
FROM mahasiswa,dosen
where dosen.id_dosen=mahasiswa.id_dosen";
$qry=mysql_query($sql, $koneksi) or die ("Gagal query".mysql_error());
$no=0;
?>
<html>
<head><title>Daftar Data Mahasiswa</title>
</head>
<body>
<table width="600" border="1" cellpadding="2" cellspacing="2" bgcolor="#CCCCCC">
<tr>
<td colspan="4" align="center" bgcolor="#CCFF33"><b>DAFTAR DATA MAHASISWA</b></td>
</tr>

<tr>
<td width="90" bgcolor="#CCFF99"><b>NIM</b></td>
<td width="311" bgcolor="#CCFF99"><b>Nama</b></td>
<td width="311" bgcolor="#CCFF99"><b>Dosen Wali</b></td>
<td width="115" align="center" bgcolor="#CCFF99"><b>Oprasi</b></td>
</tr>
<?php
while ($data=mysql_fetch_array($qry)) {
$no++;
?>
<tr bgcolor="#FFFFFF">
<td><?php echo $data['nim']; ?></td>
<td><?php echo $data['nama']; ?></td>
<td><?php echo $data['nama_dosen']; ?></td>
<td align="center">
<a href="ubah.php?nimubah=<?php echo $data['nim']; ?>">Ubah</a> | <a href="Hapus.php?nimhapus=<?php echo $data['nim']; ?>">Hapus</a>
</td>
</tr>

<?php
}

?>
</table>
<td>&nbsp;</td><td><a href="tugas.php?tampil=1"><br>Back<<</a></td></tr>

<?php
}

if($_GET['tampilmatkul']=='1') {
$sql1 = " SELECT matakuliah.kode_mk,matakuliah.nama_mk,matakuliah.bobot,dosen.nama_dosen
FROM matakuliah,dosen
where dosen.id_dosen=matakuliah.id_dosen";
$qry1=mysql_query($sql1, $koneksi) or die ("Gagal query".mysql_error());

echo('<table width="600" border="1" cellpadding="2" cellspacing="2" bgcolor="#CCCCCC">');
echo('<tr>');
echo('<td colspan="5" align="center" bgcolor="#CCFF33"><b>DAFTAR DATA MATAKULIAH</b></td>');
echo('</tr>');

echo('<tr>');
echo('<td width="180" bgcolor="#CCFF99"><b>Kode MK</b></td>');
echo('<td width="311" bgcolor="#CCFF99"><b>Nama MK</b></td>');
echo('<td width="70" bgcolor="#CCFF99"><b>Bobot</b></td>');
echo('<td width="311" bgcolor="#CCFF99"><b>Dosen Pengampu</b></td>');
echo('<td width="150" align="center" bgcolor="#CCFF99"><b>Oprasi</b></td>');
echo('</tr>');

$no1=0;
while ($data=mysql_fetch_array($qry1)) {
$no1++;

echo('<tr bgcolor="#FFFFFF">');
echo"<td>$data[kode_mk]</td>";
echo"<td>$data[nama_mk]</td>";
echo"<td align=center>$data[bobot]</td>";
echo"<td>$data[nama_dosen]</td>";
echo('<td align="center">');
echo"<a href=ubah.php?kdubah=$data[kode_mk]>Ubah</a> | <a href=Hapus.php?kdhapus=$data[kode_mk]>Hapus</a>";
echo('</td>');
echo('</tr>');


}
echo('</table>');
echo('<td>&nbsp;</td><td><a href="tugas.php?tampil=1"><br>Back<<</a></td></tr>');
}

else {

exit;
}

?>

</body>
</html>

Dalam file tugas.php di atas disertakan juga script untuk input data dan tampil data. Jika dijalankan pada web browser, tampilannya adalah sebagai berikut:

Jika kita klik menu Input Data, akan menampilkan menu input data yang terdiri dari:
- input data mahasiswa
- input data dosen
- input data matakuliah dan
- input data pengampu matkul

Tampilan dari menu input data mahasiswa:

Input data dosen:

Input data matakuliah:

Input data pengampu matkul:


Jika kita klik menu Tampil Data, terdiri dari:
- tampil data mahasiswa
- tampil data matakuliah

Tampil data mahasiswa sebagai berikut:

Tampil data matakuliah:

Selanjutnya kita buat file ubah.php untuk mengedit data:

<?php

if (! $_GET['nimubah']=="") {
$koneksi=mysql_connect('localhost','root','')or die('Koneksi Gagal');
if(! $koneksi) {
echo"Gagal koneksi boss...!";
}
mysql_select_db('tugas')or die('Database tidak di temukan');
$sql = "SELECT mahasiswa.nim,mahasiswa.nama,dosen.id_dosen FROM mahasiswa,dosen
WHERE dosen.id_dosen=mahasiswa.id_dosen and nim='".$_GET['nimubah']."'";
$qry = mysql_query($sql, $koneksi) or die ("Gagal sql data mahasiswa".mysql_error());
$data = mysql_fetch_array($qry);
$Nim = $data['nim'];
$Nama = $data['nama'];
$Dosen = $data['id_dosen'];

?>
<html>
<head>
<title>Ubah Data Mahasiswa</title>

<body>
<form name="form1" method="POST" action="ubahsim.php">
<table width="450" border="1" cellspacing="1" cellpadding="1">
<tr bgcolor="#CCFF66">
<td colspan="2" align="center"><b>UBAH DATA MAHASISWA</b></td>
</tr>
<tr>
<td>NIM </td>
<td>
<input name="txtnim" type="text" size="10" maxlength="8" value="<?php echo $Nim; ?>" disabled>
<input name="txtnim" type="hidden" value="<?php echo $Nim; ?>">
</td>
</tr>
<tr>
<td>Nama Mahasiswa </td>
<td><input name="txtnama" type="text" value="<?php echo $Nama; ?>" size="40" maxlength="30"></td>
</tr>
<tr>
<td>ID Dosen Wali </td>
<td><input name="txtiddosen" type="text" value="<?php echo $Dosen; ?>" size="40" maxlength="30"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input name="TbUbahMhs" type="submit" value="Ubah"></td>
</tr>
</table>
</form>
<?php
}

if (! $_GET['kdubah']=="") {
$koneksi=mysql_connect('localhost','root','')or die('Koneksi Gagal');
if(! $koneksi) {
echo"Gagal koneksi boss...!";
}
mysql_select_db('tugas')or die('Database tidak di temukan');
$sql = "SELECT matakuliah.kode_mk,matakuliah.nama_mk,matakuliah.bobot,dosen.id_dosen
FROM matakuliah,dosen WHERE dosen.id_dosen=matakuliah.id_dosen and kode_mk='".$_GET['kdubah']."'";
$qry = mysql_query($sql, $koneksi) or die ("Gagal sql data matakuliah".mysql_error());
$data = mysql_fetch_array($qry);
$Kode = $data['kode_mk'];
$Nama = $data['nama_mk'];
$Bobot = $data['bobot'];
$IdDosen = $data['id_dosen'];

?>
<title>Ubah Data Matakuliah</title>

<body>
<form name="form1" method="POST" action="ubahsim.php">
<table width="450" border="1" cellspacing="1" cellpadding="1">
<tr bgcolor="#CCFF66">
<td colspan="2" align="center"><b>UBAH DATA MATAKULIAH</b></td>
</tr>
<tr>
<td>Kode MK </td>
<td>
<input name="txtkodemk" type="text" size="10" maxlength="8" value="<?php echo $Kode; ?>" disabled>
<input name="txtkodemk" type="hidden" value="<?php echo $Kode; ?>">
</td>
</tr>
<tr>
<td>Nama MK </td>
<td><input name="txtnamamk" type="text" value="<?php echo $Nama; ?>" size="40" maxlength="30"></td>
</tr>
<tr>
<td>Bobot </td>
<td><input name="txtbobot" type="text" value="<?php echo $Bobot; ?>" size="40" maxlength="30"></td>
</tr>
<tr>
<td>ID Dosen Wali </td>
<td><input name="txtiddosen" type="text" value="<?php echo $IdDosen; ?>" size="40" maxlength="30"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input name="TbUbahMK" type="submit" value="Ubah"></td>
</tr>
</table>
</form>
<?php
}
?>
</body>
</head>
</html>

Jika kita ingin mengedit data mahasiswa, tampilannya adalah sebagai berikut:


Selanjutnya kita buat file ubahsim.php untuk menyimpan data yang telah diedit:

<?php
if ($_POST['TbUbahMhs']) {
$txtnim = $_POST['txtnim'];
$txtnama = $_POST['txtnama'];
$txtiddosen = $_POST['txtiddosen'];

if (trim($txtnim)=="") {
$pesan[] = "Data Nim Kosong";
}
if (trim($txtnama)=="") {
$pesan[] = "Data Nama Mahasiswa kosong";
}
if (trim($txtiddosen)=="") {
$pesan[] = "Data ID dosen kosong";
}

if (! count($pesan)==0 ) {
$Nim = $_POST['txtnim'];
$Nama = $_POST['txtnama'];
$Dosen = $_POST['txtiddosen'];

include "Ubah.php";

echo "<b> Kesalahan Input : </b><br>";
foreach ($pesan as $indeks=>$pesan_tampil) {
$urut_pesan++;
echo "<font color='#FF0000'>";
echo "$urut_pesan . $pesan_tampil <br>";
echo "</font>";
}
exit;

}
else {
$koneksi=mysql_connect('localhost','root','')or die('Koneksi Gagal');
if(! $koneksi) {
echo"Gagal koneksi boss...!";
}
mysql_select_db('tugas')or die('Database tidak di temukan');
$sql = " UPDATE mahasiswa SET
nim ='$txtnim',
nama ='$txtnama',
id_dosen ='$txtiddosen'
WHERE nim='$txtnim'";
mysql_query($sql, $koneksi)
or die ("Gagal query simpan".mysql_error());

echo "Semua benar, proses update berhasil<br>";
echo ('<a href="tugas.php?tampilmhs=1">Lihat Data Mahasiswa</a>');
}
}

if ($_POST['TbUbahMK']) {
$txtkodemk = $_POST['txtkodemk'];
$txtnamamk = $_POST['txtnamamk'];
$txtbobot = $_POST['txtbobot'];
$txtiddosen = $_POST['txtiddosen'];

if (trim($txtkodemk)=="") {
$pesan[] = "Data kode MK Kosong";
}
if (trim($txtnamamk)=="") {
$pesan[] = "Data Nama MK kosong";
}
if (trim($txtiddosen)=="") {
$pesan[] = "Data ID dosen kosong";
}

if (! count($pesan)==0 ) {
$Kode = $_POST['txtkodemk'];
$Nama = $_POST['txtnamamk'];
$Dosen = $_POST['txtiddosen'];

include "Ubah.php";

echo "<b> Kesalahan Input : </b><br>";
foreach ($pesan as $indeks=>$pesan_tampil) {
$urut_pesan++;
echo "<font color='#FF0000'>";
echo "$urut_pesan . $pesan_tampil <br>";
echo "</font>";
}
exit;
}
else {
$koneksi=mysql_connect('localhost','root','')or die('Koneksi Gagal');
if(! $koneksi) {
echo"Gagal koneksi boss...!";
}
mysql_select_db('tugas')or die('Database tidak di temukan');
$sql = " UPDATE matakuliah SET
kode_mk ='$txtkodemk',
nama_mk ='$txtnamamk',
bobot ='$txtbobot',
id_dosen ='$txtiddosen'
WHERE kode_mk='$txtkodemk'";
mysql_query($sql, $koneksi)
or die ("Gagal query simpan".mysql_error());

echo "Semua benar, proses update berhasil<br>";
echo ('<a href="tugas.php?tampilmatkul=1">Lihat Data Matakuliah</a>');
}
}
else {

exit;
}
?>

Untuk oprasi hapus data, kita buat file hapus.php:


<?php
if (! $_GET['nimhapus']=="") {
$koneksi=mysql_connect('localhost','root','')or die('Koneksi Gagal');
if(! $koneksi) {
echo"Gagal koneksi boss...!";
}
mysql_select_db('tugas')or die('Database tidak di temukan');
$sql = " DELETE FROM mahasiswa
WHERE nim ='".$_GET['nimhapus']."'";
mysql_query($sql, $koneksi)
or die ("Gagal query hapus".mysql_error());

echo "Data mahasiswa berhasil dihapus<br>";
echo ('<a href="tugas.php?tampilmhs=1">Lihat Data Mahasiswa</a>');

}

if (! $_GET['kdhapus']=="") {
$koneksi=mysql_connect('localhost','root','')or die('Koneksi Gagal');
if(! $koneksi) {
echo"Gagal koneksi boss...!";
}
mysql_select_db('tugas')or die('Database tidak di temukan');
$sql = " DELETE FROM matakuliah
WHERE kode_mk ='".$_GET['kdhapus']."'";
mysql_query($sql, $koneksi)
or die ("Gagal query hapus".mysql_error());

echo "Data mahasiswa berhasil dihapus<br>";
echo ('<a href="tugas.php?tampilmatkul=1">Lihat Data Matakuliah</a>');

}
else {
exit;
}
?>
Share this article :
 
Support : Creating Website | Johny Template | Mas Template
Copyright © 2011. INSPIRATION - All Rights Reserved
Template Created by Creating Website Published by Mas Template
Proudly powered by Blogger