0% found this document useful (0 votes)
34 views6 pages

Listing Praktikum Pengembangan Aplikasi Web Pertemuan Ke-8: 1. Barang - Isi - PHP

The document describes the 8th meeting of a web application development practical. It discusses two PHP files, barang_isi.php and barang_simpan.php, that allow adding product data to a database. barang_isi.php contains a form to input product name, price, stock, and photo. barang_simpan.php validates the input, uploads the photo, inserts the data to the database, and creates a thumbnail. It then discusses similar mahasiswa_isi.php and mahasiswa_simpan.php files for adding student data.

Uploaded by

vitalis fashion
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views6 pages

Listing Praktikum Pengembangan Aplikasi Web Pertemuan Ke-8: 1. Barang - Isi - PHP

The document describes the 8th meeting of a web application development practical. It discusses two PHP files, barang_isi.php and barang_simpan.php, that allow adding product data to a database. barang_isi.php contains a form to input product name, price, stock, and photo. barang_simpan.php validates the input, uploads the photo, inserts the data to the database, and creates a thumbnail. It then discusses similar mahasiswa_isi.php and mahasiswa_simpan.php files for adding student data.

Uploaded by

vitalis fashion
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Nama : vitalis agung

NIM : 145410090

LISTING PRAKTIKUM PENGEMBANGAN APLIKASI WEB

PERTEMUAN KE-8

A. PRAKTIK
1. barang_isi.php
<h2>.:: ISI BARANG ::.</h2>
<form action="barang_simpan.php" method="post" enctype="multipart/form-data">
<table border="1">
<tr>
<td>Nama Barang</td>
<td><input type="text" name="nama"/></td>
</tr>
<tr>
<td>Harga Jual</td>
<td><input type="text" name="harga"/></td>
</tr>
<tr>
<td>Stok</td>
<td><input type="text" name="stok"/></td>
</tr>
<tr>
<td>Gambar [max=1.5MB]</td>
<td><input type="file" name="foto"/></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Simpan" name="proses"/>
<input type="reset" value="Reset" name="proses"/>
</td>
</tr>
</table>
</form>

2. barang_simpan.php
<?php
$nama = $_POST['nama'];
$harga = $_POST['harga'];
$stok = $_POST['stok'];

$foto = $_FILES['foto']['name'];
$tmpName = $_FILES['foto']['tmp_name'];
$size = $_FILES['foto']['size'];
$type = $_FILES['foto']['type'];

$maxsize = 1500000;
$typeYgBoleh = array("image/jpeg","image/png","image/pjpeg");

$dirFoto = "pict";
if(!is_dir($dirFoto))
mkdir($dirFoto);

$fileTujuanFoto = $dirFoto."/".$foto;

$dirThumb = "thumb";
if(!is_dir($dirThumb))
mkdir($dirThumb);
Nama : vitalis agung
NIM : 145410090

$fileTujuanThumb = $dirThumb."/t_".$foto;

$dataValid = "YA";

if($size > 0){


if($size > $maxsize){
echo "Ukuran File Terlalu Besar <br/>";
$dataValid = "TIDAK";
}
if(!in_array($type, $typeYgBoleh)){
echo "Type File Tidak Dikenal <br/>";
$dataValid = "TIDAK";
}
}

if(strlen(trim($nama)) == 0){
echo "Nama Barang Harus Diisi! <br/>";
$dataValid = "TIDAK";
}

if(strlen(trim($harga)) == 0){
echo "Harga Harus Diisi! <br/>";
$dataValid = "TIDAK";
}

if(strlen(trim($stok)) == 0){
echo "Stok Harus Diisi! <br/>";
$dataValid = "TIDAK";
}

if($dataValid == "TIDAK"){
echo "Masih Ada Kesalahan, silahkan perbaiki! <br/>";
echo "<input type='button' value='Kembali'
onClick='self.history.back()'>";
exit;
}

include "koneksi.php";

$sql = "insert into barang (nama, harga, stok, foto)


values ('$nama','$harga','$stok','$foto')";

$hasil = mysqli_query($kon, $sql);

if(!$hasil){
echo "Gagal Simpan, silahkan diulangi! <br/>";
echo mysqli_error($kon);
echo "<br/> <input type='button' value='Kembali'
onClick='self.history.back()'>";
exit;
} else{
echo "Simpan data berhasil";
}

if($size > 0){


if(!move_uploaded_file($tmpName, $fileTujuanFoto)){
Nama : vitalis agung
NIM : 145410090

echo "Gagal Upload Gambar... <br/>";


echo "<a href='barang_tampil.php'>Daftar Barang</a>";
exit;
}
else{
buat_thumbnail($fileTujuanFoto, $fileTujuanThumb);
}
}

echo "<br/>File sudah di upload. <br/>";

function buat_thumbnail($file_src, $file_dst){


list($w_src, $h_src, $type) = getImageSize($file_src);

switch($type){
case 1 : $img_src = imagecreatefromgif($file_src);break;
case 2 : $img_src =
imagecreatefromjpeg($file_src);break;
case 3 : $img_src = imagecreatefrompng($file_src);break;
}

$thumb = 100;
if($w_src > $h_src){
$w_dst = $thumb;
$h_dst = round($thumb / $w_src * $h_src);
}
else{
$w_dst = round($thumb / $w_src * $h_src);
$h_dst = $thumb;
}

$img_dst = imagecreatetruecolor($w_dst, $h_dst);

imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $w_dst,


$h_dst, $w_src, $h_src);
imagejpeg($img_dst, $file_dst);
imagedestroy($img_src);
imagedestroy($img_dst);
}

?>

B. LATIHAN
1. Mahasiswa_isi.php
<h2>.:: ISI BARANG ::.</h2>
<form action="mahasiswa_simpan.php" method="post"
enctype="multipart/form-data">
<table border="1">
Nama : vitalis agung
NIM : 145410090

<tr>
<td>NIM</td>
<td><input type="text" name="nim"/></td>
</tr>
<tr>
<td>Nama</td>
<td><input type="text" name="nama"/></td>
</tr>
<tr>
<td>Jenis Kelamin</td>
<td><input type="text" name="jk"/></td>
</tr>
<tr>
<td>Jurusan</td>
<td><input type="text" name="jurusan"/></td>
</tr>
<tr>
<td>Alamat</td>
<td><input type="text" name="alamat"/></td>
</tr>
<tr>
<td>Gambar [max=1.5MB]</td>
<td><input type="file" name="foto"/></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Simpan"
name="proses"/>
<input type="reset" value="Reset" name="proses"/>
</td>
</tr>
</table>
</form>
2. Mahasiswa_simpan.php
<?php
$nim = $_POST['nim'];
$nama = $_POST['nama'];
$jk = $_POST['jk'];
$jurusan = $_POST['jurusan'];
$alamat = $_POST['alamat'];

$foto = $_FILES['foto']['name'];
$tmpName = $_FILES['foto']['tmp_name'];
$size = $_FILES['foto']['size'];
$type = $_FILES['foto']['type'];

$maxsize = 1500000;
$typeYgBoleh = array("image/jpeg","image/png","image/pjpeg");

$dirFoto = "pict";
if(!is_dir($dirFoto))
mkdir($dirFoto);

$fileTujuanFoto = $dirFoto."/".$foto;

$dirThumb = "thumb";
if(!is_dir($dirThumb))
mkdir($dirThumb);

$fileTujuanThumb = $dirThumb."/t_".$foto;
Nama : vitalis agung
NIM : 145410090

$dataValid = "YA";

if($size > 0){


if($size > $maxsize){
echo "Ukuran File Terlalu Besar <br/>";
$dataValid = "TIDAK";
}
if(!in_array($type, $typeYgBoleh)){
echo "Type File Tidak Dikenal <br/>";
$dataValid = "TIDAK";
}
}

if(strlen(trim($nim)) == 0){
echo "Nim Mahasiswa Harus Diisi! <br/>";
$dataValid = "TIDAK";
}

if(strlen(trim($nama)) == 0){
echo "Nama Mahasiswa Harus Diisi! <br/>";
$dataValid = "TIDAK";
}

if(strlen(trim($jk)) == 0){
echo "Jenis Kelamin Harus Diisi! <br/>";
$dataValid = "TIDAK";
}

if(strlen(trim($jurusan)) == 0){
echo "Jurusan Harus Diisi! <br/>";
$dataValid = "TIDAK";
}

if(strlen(trim($alamat)) == 0){
echo "Alamat Harus Diisi! <br/>";
$dataValid = "TIDAK";
}
if($dataValid == "TIDAK"){
echo "Masih Ada Kesalahan, silahkan perbaiki! <br/>";
echo "<input type='button' value='Kembali'
onClick='self.history.back()'>";
exit;
}
include "koneksi_latihan.php";

$sql = "insert into mahasiswa (nim, nama, jk, jurusan, alamat, foto)
values
('$nim','$nama','$jk','$jurusan','$alamat','$foto')";

$hasil = mysqli_query($kon, $sql);

if(!$hasil){
echo "Gagal Simpan, silahkan diulangi! <br/>";
echo mysqli_error($kon);
echo "<br/> <input type='button' value='Kembali'
onClick='self.history.back()'>";
Nama : vitalis agung
NIM : 145410090

exit;
} else{
echo "Simpan data berhasil";
}

if($size > 0){


if(!move_uploaded_file($tmpName, $fileTujuanFoto)){
echo "Gagal Upload Gambar... <br/>";
echo "<a href='barang_tampil.php'>Daftar Barang</a>";
exit;
}
else{
buat_thumbnail($fileTujuanFoto, $fileTujuanThumb);
}
}
echo "<br/>File sudah di upload. <br/>";
function buat_thumbnail($file_src, $file_dst){
list($w_src, $h_src, $type) = getImageSize($file_src);

switch($type){
case 1 : $img_src = imagecreatefromgif($file_src);break;
case 2 : $img_src =
imagecreatefromjpeg($file_src);break;
case 3 : $img_src = imagecreatefrompng($file_src);break;
}

$thumb = 100;
if($w_src > $h_src){
$w_dst = $thumb;
$h_dst = round($thumb / $w_src * $h_src);
}
else{
$w_dst = round($thumb / $w_src * $h_src);
$h_dst = $thumb;
}
$img_dst = imagecreatetruecolor($w_dst, $h_dst);
imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $w_dst,
$h_dst, $w_src, $h_src);
imagejpeg($img_dst, $file_dst);
imagedestroy($img_src);
imagedestroy($img_dst);
}
?>

You might also like