Script Latihan WP Data Handphone
Buat temen-temen BSI, ini ada script latihan Web Programming I.
Sebelumnya kalian buat dulu sebuah folder dengan nama "barang" di direktori C:\xampp\htdocs, bagi yang menggunakan web server xampp.
Kemudian buat folder "gambarhp" di didalam folder "barang" tadi (fungsi folder untuk menyimpan gambar yang diinput.
Persiapan berikutnya adalah dengan membuat database menggunakan phpmyadmin.
Buat database dengan nama: barang.
Buat tabel dengan nama: handphone
struktur tabel:
kdbarang varchar(6) primary key
nmbarang varchar(20)
gambar varchar(100)
Kemudian buatlah script-script di bawah ini:
Script: tambah.php
<!doctype html public "-//W3C//DTD HTML 4.0 //EN">
<html>
<head>
<title>Jual Beli HP</title>
</head>
<body>
<form action="simpan.php" method=post enctype="multipart/form-data">
<table border=1>
<tr><td colspan=3 align=center>Input Data Handphone</td></tr>
<tr>
<td>Kode Barang</td>
<td>:</td>
<td><input type=text name=tkd></td>
</tr>
<tr>
<td>Nama Barang</td>
<td>:</td>
<td><input type=text name=tnm></td>
</tr>
<tr>
<td>Gambar</td>
<td>:</td>
<td><input type=file name=tgb></td>
</tr>
<tr>
<td colspan=3 align=center><input type=submit value=SIMPAN><input type=reset value=BATAL></td>
</tr>
</table>
</form>
</body>
</html>
Script koneksi.php
<?php
$koneksi=mysql_connect("localhost","root","")or die(mysql_error());
if($koneksi)
{
mysql_select_db("barang")or die(mysql_error());
}
else
{
echo"<script>alert('KONEKSI GAGAL');
</script>";
}
?>
Script simpan.php
<?php
include "koneksi.php";
$folder=$_FILES['tgb']['tmp_name'];
$gambar=$_FILES['tgb']['name'];
$kd=$_POST['tkd'];
$nm=$_POST['tnm'];
$simpan=mysql_query("insert into handphone values('$kd', '$nm', '$gambar')" )
or die(mysql_error());
move_uploaded_file($folder,"gambarhp/$gambar");
if($simpan)
{
include "tampil.php";
echo"<script>alert('DATA BERHASIL DISIMPAN');</script>";
}
else
{
echo"<script>alert('DATA GAGAL DISIMPAN');</script>";
}
?>
Script tampil.php
<!doctype html public "-//W3C//DTD HTML 4.0 //EN">
<html>
<head>
<title>Data Handphone</title>
</head>
<body>
<table border=1 align=center>
<tr><td colspan=3 align=center>Tampil Data Handphone</td></tr>
<tr>
<td align=center>Kode Barang</td>
<td align=center>Nama Barang</td>
<td align=center>Gambar</td>
</tr>
<?php
include "koneksi.php";
$tampil=mysql_query("select*from handphone")or die(mysql_error());
while($hp=mysql_fetch_array($tampil))
{
echo"
<tr>
<td>$hp[0]</td>
<td>$hp[1]</td>
<td><img src='gambarhp/$hp[2]' width=80px height=50px></td>
</tr>
";}
?>
<tr>
<td colspan=3 align=center><a href=tambah.php><input type=submit value=KEMBALI></a></td>
</tr>
</table>
</body>
</html>
Sebelumnya kalian buat dulu sebuah folder dengan nama "barang" di direktori C:\xampp\htdocs, bagi yang menggunakan web server xampp.
Kemudian buat folder "gambarhp" di didalam folder "barang" tadi (fungsi folder untuk menyimpan gambar yang diinput.
Persiapan berikutnya adalah dengan membuat database menggunakan phpmyadmin.
Buat database dengan nama: barang.
Buat tabel dengan nama: handphone
struktur tabel:
kdbarang varchar(6) primary key
nmbarang varchar(20)
gambar varchar(100)
Kemudian buatlah script-script di bawah ini:
Script: tambah.php
<!doctype html public "-//W3C//DTD HTML 4.0 //EN">
<html>
<head>
<title>Jual Beli HP</title>
</head>
<body>
<form action="simpan.php" method=post enctype="multipart/form-data">
<table border=1>
<tr><td colspan=3 align=center>Input Data Handphone</td></tr>
<tr>
<td>Kode Barang</td>
<td>:</td>
<td><input type=text name=tkd></td>
</tr>
<tr>
<td>Nama Barang</td>
<td>:</td>
<td><input type=text name=tnm></td>
</tr>
<tr>
<td>Gambar</td>
<td>:</td>
<td><input type=file name=tgb></td>
</tr>
<tr>
<td colspan=3 align=center><input type=submit value=SIMPAN><input type=reset value=BATAL></td>
</tr>
</table>
</form>
</body>
</html>
Script koneksi.php
<?php
$koneksi=mysql_connect("localhost","root","")or die(mysql_error());
if($koneksi)
{
mysql_select_db("barang")or die(mysql_error());
}
else
{
echo"<script>alert('KONEKSI GAGAL');
</script>";
}
?>
Script simpan.php
<?php
include "koneksi.php";
$folder=$_FILES['tgb']['tmp_name'];
$gambar=$_FILES['tgb']['name'];
$kd=$_POST['tkd'];
$nm=$_POST['tnm'];
$simpan=mysql_query("insert into handphone values('$kd', '$nm', '$gambar')" )
or die(mysql_error());
move_uploaded_file($folder,"gambarhp/$gambar");
if($simpan)
{
include "tampil.php";
echo"<script>alert('DATA BERHASIL DISIMPAN');</script>";
}
else
{
echo"<script>alert('DATA GAGAL DISIMPAN');</script>";
}
?>
Script tampil.php
<!doctype html public "-//W3C//DTD HTML 4.0 //EN">
<html>
<head>
<title>Data Handphone</title>
</head>
<body>
<table border=1 align=center>
<tr><td colspan=3 align=center>Tampil Data Handphone</td></tr>
<tr>
<td align=center>Kode Barang</td>
<td align=center>Nama Barang</td>
<td align=center>Gambar</td>
</tr>
<?php
include "koneksi.php";
$tampil=mysql_query("select*from handphone")or die(mysql_error());
while($hp=mysql_fetch_array($tampil))
{
echo"
<tr>
<td>$hp[0]</td>
<td>$hp[1]</td>
<td><img src='gambarhp/$hp[2]' width=80px height=50px></td>
</tr>
";}
?>
<tr>
<td colspan=3 align=center><a href=tambah.php><input type=submit value=KEMBALI></a></td>
</tr>
</table>
</body>
</html>