LAPORAN
PRAKTIKUM
Pemograman WEB 1
Modul 9
“PHP CRUD”
Nama : Radea Nurlaila
NIM :
2015091083
Prodi :
SI 2015 C
LABORATORIUM
KOMPUTER
FAKULTAS
ILMU KOMPUTER
UNIVERSITAS
KUNINGAN
2017
Jl.
Cut NyakDien, No.36A CijohoKuninganTelp/Faks : 0232-974824
1. PRAKTIKUM
Membuat script php dan MySQL untuk menampilkan tabel mahasiswa ke dalam web1.Buat database dengan nama db_fkom, kemudian buat tabel mahasiswa seperti berikut
2. Setelah tabel dibuat, lalu buat script koneksi.php untuk mengkoneksikan web dengan database
<?php
$db_host = "localhost";
$db_user = "root";
$db_pass = "";
$koneksi = mysql_connect("$db_host", "$db_user", "$db_pass");
$sambung = mysql_select_db("db_fkom",$koneksi);
3. Setelah itu, buat script index.php
<title> Modul 9 </title>
<table width="70%" height="100%" border="2" align="center">
<tr height="20%" border="0">
<td align="center" width="33%"><a href="data_mhs.php"> Data Mahasiswa </a></td>
<td align="center" width="33%"><a href="data_mk.php"> Data Mata Kuliah </a></td>
<td align="center" width="33%"><a href="data_prodi.php"> Data Prodi </a></td>
</tr>
<tr height="70%">
<td colspan="3">
<h2 align="center"> Welcome!</h2>
</td>
</tr>
<tr height="10%">
<td colspan="3">
<center> © Radea Nurlaila </center></td>
</tr>
</table>
4. Setelah file index dibuat, buat file data_mhs.php yang akan digunakan untuk menampilkan data dari tabel mahasiswa
<title> Modul 9 </title>
<table width="70%" height="100%" border="2" align="center">
<tr height="20%" border="0">
<td align="center" width="33%"><a href="data_mhs.php"> Data Mahasiswa </a></td>
<td align="center" width="33%"><a href="data_mk.php"> Data Mata Kuliah </a></td>
<td align="center" width="33%"><a href="data_prodi.php"> Data Prodi </a></td>
</tr>
<tr height="70%" valign="top">
<td colspan="3">
<form id="form1" name="form1" method="post" action="">
<table width="50%" border="2" align="center" valign="top">
<tr>
<td width="20%" align="center"><b> NIM </b></td>
<td width="60%" align="center"><b> Nama </b></td>
<td width="20%" align="center"><b> Jenis Kelamin </b></td>
<td> Aksi</td>
</tr>
<?php
include "koneksi.php";
$query="select * from mahasiswa";
$result=mysql_query($query);
while ($data=mysql_fetch_array($result)) {
?>
<tr>
<td align="center"><a href="edit_mhs.php?nim=<?php echo $data['nim'];?>"><?php echo $data['nim']; ?></a></td>
<td align="center"><?php echo $data['nama']; ?></td>
<td align="center"><?php echo $data['jk']; ?></td>
<td align="center"><a href="delete_mhs.php?nim=<?php echo $data['nim'];?>" onclick="return confirm('Apakah Anda Yakin akan menghapus data ini?')" title="delete">Delete</a></td>
</tr>
<?php
}
?>
<center><a href="input_mhs.php"> Tambah Data </a></center></table>
</form></td>
</tr>
<tr height="10%">
<td colspan="3">
<center> © Radea Nurlaila </center></td>
</tr>
</table>
5. Lalu buat file input_mhs.php untuk menampilkan form yang akan digunakan untuk meng-inputkan data ke dalam tabel mahasiswa
<title> Modul 9 </title>
<table width="70%" height="100%" border="2" align="center">
<tr height="20%" border="0">
<td align="center" width="33%"><a href="data_mhs.php"> Data Mahasiswa </a></td>
<td align="center" width="33%"><a href="data_mk.php"> Data Mata Kuliah </a></td>
<td align="center" width="33%"><a href="data_prodi.php"> Data Prodi </a></td>
</tr>
<tr height="70%" valign="top">
<td colspan="3">
<form name="input" method="post" action="input_mhs.php">
<table border="0" align="center">
<tr>
<td> NIM</td>
<td><input type="text" name="nim" size="20" maxlength="15"></td>
</tr>
<tr>
<td> Nama</td>
<td><input type="text" name="nama" size="40" maxlength="30"></td>
</tr>
<tr>
<td> Jenis Kelamin</td>
<td><input type="radio" name="jk" value="L" id="jk1">L
<input type="radio" name="jk" value="P" id="jk2">P</td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="simpan" value="Simpan"></td>
</tr>
<?php
extract($_POST);
if(isset($simpan)) {
include "koneksi.php";
$input="insert into mahasiswa values('$nim','$nama','$jk')";
$save=mysql_query($input);
if($save) {
?>
<script lang="javascript">
alert("Data Berhasil dimasukan");
document.location="data_mhs.php";
</script>
<?php
} else {
?>
<script lang="javascript">
alert("Data Gagal dimasukan");
document.location="input_mhs.php";
</script>
<?php
}
}
?>
<center> Form Input Data Mahasiswa </center></table>
</form></td>
</tr>
<tr height="10%">
<td colspan="3">
<center> © Radea Nurlaila </center></td>
</tr>
</table>
6. Lalu buat file edit_mhs.php untuk mengedit data yang ada dalam tabel mahasiswa
<?php
include "koneksi.php";
$query="select * from mahasiswa where nim='$_GET[nim]'";
$result=mysql_query($query);
while ($data=mysql_fetch_array($result)) {
$n=$data['nim'];
$nm=$data['nama'];
$j=$data['jk'];
}
?>
<title> Modul 9 </title>
<table width="70%" height="100%" border="2" align="center">
<tr height="20%" border="0">
<td align="center" width="33%"><a href="data_mhs.php"> Data Mahasiswa </a></td>
<td align="center" width="33%"><a href="data_mk.php"> Data Mata Kuliah </a></td>
<td align="center" width="33%"><a href="data_prodi.php"> Data Prodi </a></td>
</tr>
<tr height="70%" valign="top">
<td colspan="3">
<form name="edit" method="post" action="edit_mhs.php">
<table border="0" align="center">
<tr>
<td> NIM</td>
<td> : <input type="text" name="nim" size="20" maxlenght="15" value="<?php echo $n; ?>" readonly></td>
</tr>
<tr>
<td> Nama</td>
<td> : <input type="text" name="nama" size="40" maxlenght="30" value="<?php echo $nm; ?>"></td>
</tr>
<tr>
<td> Jenis Kelamin</td>
<?php
if($j=="L") {
?>
<td> : <input type="radio" name="jk" value="L" id="jk1" checked>L
<input type="radio" name="jk" value="P" id="jk2">P</td>
<?php
} else {
?>
<td> : <input type="radio" name="jk" value="L" id="jk1">L
<input type="radio" name="jk" value="P" id="jk2" checked>P</td>
<?php
}
?></tr>
<tr>
<td colspan="2"><input type="submit" name="edit" value="Edit"></tr>
<?php
extract($_POST);
if(isset($edit)) {
$input="update mahasiswa set nama='$nama', jk='$jk' where nim='$nim'";
$save=mysql_query($input);
if($save) {
?>
<script lang="javascript">
alert("Data Berhasil Diupdate");
document.location="data_mhs.php";
</script>
<?php
} else {
?>
<script lang="javascript">
alert("Data Gagal Diupdate");
document.location="edit_mhs.php";
</script>
<?php
}
}
?>
<center> Form Edit Data Mahasiswa </center></table>
</form></td>
</tr>
<tr height="10%">
<td colspan="3">
<center> © Radea Nurlaila </center></td>
</tr>
</table>
7. Dan yang terakhir buat file delete_mhs.php untuk menghapus data dalam tabel mahasiswa
<?php
include "koneksi.php";
$query="delete from mahasiswa where nim='$_GET[nim]'";
$delete=mysql_query($query);
if($delete) {
?>
<script lang="javascript">
alert("Data Berhasil Dihapus");
document.location="data_mhs.php";
</script>
<?php
} else {
?>
<script lang="javascript">
alert("Data Gagal Dihapus");
document.location="data_mhs.php";
</script>
<?php
}
?>
8. Run Program, maka hasilnya adalah
TUGAS
Tambahkan tabel matkul dalam database lalu buat script php dan MySQL untuk menampilkan tabel dan menambah data ke dalam tabel1. Pertama buat database matkul
2. Buat file data_mk.php , scriptnya adalah :
<title> Modul 9 </title>
<table width="70%" height="100%" border="2" align="center">
<tr height="20%" border="0">
<td align="center" width="33%"><a href="data_mhs.php"> Data Mahasiswa </a></td>
<td align="center" width="33%"><a href="data_mk.php"> Data Mata Kuliah </a></td>
<td align="center" width="33%"><a href="data_prodi.php"> Data Prodi </a></td>
</tr>
<tr height="70%" valign="top">
<td colspan="3">
<form id="form1" name="form1" method="post" action="">
<table width="50%" border="2" align="center" valign="top">
<tr>
<td width="40%" align="center"><b> Kode Mata Kuliah </b></td>
<td width="40%" align="center"><b> Nama Mata Kuliah </b></td>
<td width="20%" align="center"><b> Semester </b></td>
</tr>
<?php
include "koneksi.php";
$query="select * from matkul";
$result=mysql_query($query);
while ($data=mysql_fetch_array($result)) {
?>
<tr>
<td align="center"><?php echo $data['kd_mk']; ?></td>
<td align="center"><?php echo $data['nama_mk']; ?></td>
<td align="center"><?php echo $data['semester']; ?></td>
</tr>
<?php
}
?>
<center><a href="input_mk.php"> Tambah Data </a></center></table>
</form></td>
</tr>
<tr height="10%">
<td colspan="3">
<center> © Radea Nurlaila </center></td>
</tr>
</table>
3. Setelah itu buat file input_mk.php untuk menambahkan data ke dalam tabel matkul
<title> Modul 9 </title>
<table width="70%" height="100%" border="2" align="center">
<tr height="20%" border="0">
<td align="center" width="33%"><a href="data_mhs.php"> Data Mahasiswa </a></td>
<td align="center" width="33%"><a href="data_mk.php"> Data Mata Kuliah </a></td>
<td align="center" width="33%"><a href="data_prodi.php"> Data Prodi </a></td>
</tr>
<tr height="70%" valign="top">
<td colspan="3">
<form name="input" method="post" action="input_mk.php">
<table border="0" align="center">
<tr>
<td> Kode Mata Kuliah</td>
<td><input type="text" name="kd_mk" size="40" maxlength="15"></td>
</tr>
<tr>
<td> Nama Mata Kuliah</td>
<td><input type="text" name="nama_mk" size="40" maxlength="30"></td>
</tr>
<tr>
<td> Semester</td>
<td><input type="text" name="semester" size="40" maxlength="30"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="simpan" value="Simpan"></td>
</tr>
<?php
extract($_POST);
if(isset($simpan)) {
include "koneksi.php";
$input="insert into matkul values('$kd_mk','$nama_mk','$semester')";
$save=mysql_query($input);
if($save) {
?>
<script lang="javascript">
alert("Data Berhasil dimasukan");
document.location="data_mk.php";
</script>
<?php
} else {
?>
<script lang="javascript">
alert("Data Gagal dimasukan");
document.location="input_mk.php";
</script>
<?php
}
}
?>
<center> Form Input Data Mata Kuliah </center></table>
</form></td>
</tr>
<tr height="10%">
<td colspan="3">
<center> © Radea Nurlaila </center></td>
</tr>
</table>
Maka Hasilnya adalah : klik tambah data
POST TEST
Tambahkan tabel prodi dalam database lalu buat script php dan MySQL untuk menampilkan tabel ke dalam web dan juga script untuk input, edit dan hapus record1. Buat tabel prodi dalam database
2. Buat file data_prodi.php
<title> Modul 9 </title>
<table width="70%" height="100%" border="2" align="center">
<tr height="20%" border="0">
<td align="center" width="33%"><a href="data_mhs.php"> Data Mahasiswa </a></td>
<td align="center" width="33%"><a href="data_mk.php"> Data Mata Kuliah </a></td>
<td align="center" width="33%"><a href="data_prodi.php"> Data Prodi </a></td>
</tr>
<tr height="70%" valign="top">
<td colspan="3">
<form id="form1" name="form1" method="post" action="">
<table width="50%" border="2" align="center" valign="top">
<tr>
<td width="30%" align="center"><b> Kode Prodi </b></td>
<td width="40%" align="center"><b> Nama Prodi </b></td>
<td width="17%" align="center"><b> Jenjang </b></td>
<td width="13%" align="center"><b> Aksi </b></td>
</tr>
<?php
include "koneksi.php";
$query="select * from prodi";
$result=mysql_query($query);
while ($data=mysql_fetch_array($result)) {
?>
<tr>
<td align="center"><a href="edit_prodi.php?kd_prodi=<?php echo $data['kd_prodi'];?>"><?php echo $data['kd_prodi']; ?></a></td>
<td align="center"><?php echo $data['nama_prodi']; ?></td>
<td align="center"><?php echo $data['jenjang']; ?></td>
<td align="center"><a href="delete_prodi.php?kd_prodi=<?php echo $data['kd_prodi'];?>" onclick="return confirm('Apakah Anda Yakin akan menghapus data ini?')" title="delete">Delete</a></td>
</tr>
<?php
}
?>
<center><a href="input_prodi.php"> Tambah Data </a></center></table>
</form></td>
</tr>
<tr height="10%">
<td colspan="3">
<center> © Radea Nurlaila </center></td>
</tr>
</table>
3. Lalu buat file input_prodi.php
<title> Modul 9 </title>
<table width="70%" height="100%" border="2" align="center">
<tr height="20%" border="0">
<td align="center" width="33%"><a href="data_mhs.php"> Data Mahasiswa </a></td>
<td align="center" width="33%"><a href="data_mk.php"> Data Mata Kuliah </a></td>
<td align="center" width="33%"><a href="data_prodi.php"> Data Prodi </a></td>
</tr>
<tr height="70%" valign="top">
<td colspan="3">
<form name="input" method="post" action="input_prodi.php">
<table border="0" align="center">
<tr>
<td> Kode Prodi</td>
<td><input type="text" name="kd_prodi" size="40" maxlength="15"></td>
</tr>
<tr>
<td> Nama Prodi</td>
<td><input type="text" name="nama_prodi" size="40" maxlength="30"></td>
</tr>
<tr>
<td> Jenjang</td>
<td><input type="text" name="jenjang" size="40" maxlength="2"></td>
</td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="simpan" value="Simpan"></td>
</tr>
<?php
extract($_POST);
if(isset($simpan)) {
include "koneksi.php";
$input="insert into prodi values('$kd_prodi','$nama_prodi','$jenjang')";
$save=mysql_query($input);
if($save) {
?>
<script lang="javascript">
alert("Data Berhasil dimasukan");
document.location="data_prodi.php";
</script>
<?php
} else {
?>
<script lang="javascript">
alert("Data Gagal dimasukan");
document.location="input_prodi.php";
</script>
<?php
}
}
?>
<center> Form Input Data Prodi </center></table>
</form></td>
</tr>
<tr height="10%">
<td colspan="3">
<center> © Radea Nurlaila </center></td>
</tr>
</table>
4. buat file edit_prodi.php
<?php
include "koneksi.php";
$query="select * from prodi where kd_prodi='$_GET[kd_prodi]'";
$result=mysql_query($query);
while ($data=mysql_fetch_array($result)) {
$kd=$data['kd_prodi'];
$nmp=$data['nama_prodi'];
$jj=$data['jenjang'];
}
?>
<title> Modul 9 </title>
<table width="70%" height="100%" border="2" align="center">
<tr height="20%" border="0">
<td align="center" width="33%"><a href="data_mhs.php"> Data Mahasiswa </a></td>
<td align="center" width="33%"><a href="data_mk.php"> Data Mata Kuliah </a></td>
<td align="center" width="33%"><a href="data_prodi.php"> Data Prodi </a></td>
</tr>
<tr height="70%" valign="top">
<td colspan="3">
<form name="edit" method="post" action="edit_prodi.php">
<table border="0" align="center">
<tr>
<td> Kode Prodi</td>
<td> : <input type="text" name="kd_prodi" size="20" maxlength="15" value="<?php echo $kd; ?>" readonly></td>
</tr>
<tr>
<td> Nama Prodi</td>
<td> : <input type="text" name="nama_prodi" size="40" maxlength="30" value="<?php echo $nmp; ?>"></td>
</tr>
<tr>
<td> Jenjang</td>
<td> : <input type="text" name="jenjang" size="40" maxlength="30" value="<?php echo $jj; ?>"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="edit" value="Edit"></tr>
<?php
extract($_POST);
if(isset($edit)) {
$input="update prodi set nama_prodi='$nama_prodi', jenjang='$jenjang' where kd_prodi='$kd_prodi'";
$save=mysql_query($input);
if($save) {
?>
<script lang="javascript">
alert("Data Berhasil Diupdate");
document.location="data_prodi.php";
</script>
<?php
} else {
?>
<script lang="javascript">
alert("Data Gagal Diupdate");
document.location="edit_prodi.php";
</script>
<?php
}
}
?>
<center> Form Edit Data Prodi </center></table>
</form></td>
</tr>
<tr height="10%">
<td colspan="3">
<center> © Radea Nurlaila </center></td>
</tr>
</table>
<?php
include "koneksi.php";
$query="delete from prodi where kd_prodi='$_GET[kd_prodi]'";
$delete=mysql_query($query);
if($delete) {
?>
<script lang="javascript">
alert("Data Berhasil Dihapus");
document.location="data_prodi.php";
</script>
<?php
} else {
?>
<script lang="javascript">
alert("Data Gagal Dihapus");
document.location="data_prodi.php";
</script>
<?php
}
?>
Maka hasilnya adalah :
0 komentar:
Posting Komentar