Thursday, April 7, 2016

Project Relation & Inner Join Mysql Php



Project Relation &  Inner Join Mysql Php



Pada postingan ini adalah tugas praktikum system basis data yaitu membuat Database :Relasi with table buku and table product.


1.      Tabel
a.       Table buku
 
b.      Table product



1.      Script php
a.     Index.php


<!DOCTYPE html>
<html>
<head>
<title>Relasi</title>
</head>
<body>
        <a href="tampil.php">Daftar Buku</a>
        <br/>
        <a href="form.php">Tambah Buku</a>
</body>
</html>
 


Hasil tampilan
 



b.    Form.php


<!DOCTYPE html>
<html>
<head>
<title>Tambah Buku</title>
</head>
<body>
<form name="form1" action="simpan.php" method="post">
    <dl>
            <dt>Nama</dt>
            <dd><input type="text" name="nama"/></dd>
            <dt>Harga</dt>
            <dd><input type="text" name="harga"/></dd>
            <dt>Harga</dt>
            <dd>
                <select name="jenis">
                        <option value="BUKU">Buku</option>
                        </select>
            </dd>
            <dt>Penulis</dt>
            <dd><input type="text" name="penulis"/></dd>
            <dt>Penerbit</dt>
            <dd><input type="text" name="penerbit"/></dd>
            <dt>Isbn</dt>
            <dd><input type="text" name="isbn"/></dd>
            <dt>Tanggal Terbit</dt>
            <dd><input type="text" name="tgl_terbit"/></dd>
            <dt></dt>
            <dd><input type="submit" value="Simpan"/></dd>
   </dl>
  
</form>
</body>
</html>
 


Hasil dari tambah buku..
1.      Record 1

2.      Record 2

3.      Record 3
 
 


b.    Tampil.php
 


<?php
$conn = mysql_connect("localhost","root","root");
mysql_select_db("dbrelasi");
$sql = "select * from product inner join buku on product.id_product=buku.id_product";
$result = mysql_query($sql);
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Relasi</title>
</head>
<body>

<table cellpadding="5" cellspacing="0" border="1">
  <tr>
         <th>Nama</th>
         <th>Harga</th>
         <th>Penulis</th>
         <th>Penerbit</th>
         <th>ISBN</th>
         <th>Tanggal Terbit</th>
  </tr>
 <?php while ($buku = mysql_fetch_array ($result)){?>
  <tr>
      <td><?php echo $buku['nama']?></td>
      <td><?php echo $buku['harga']?></td>
      <td><?php echo $buku['penulis']?></td>
      <td><?php echo $buku['penerbit']?></td>
      <td><?php echo $buku['isbn']?></td>
      <td><?php echo $buku['tgl_terbit']?></td>
  </tr>
 <?php }?>
</table>
</body>
</html>


                 


Hasil dari tampil.php
 

 
 



b.    Simpan.php
 

<?php
if ($_POST) {
$conn = mysql_connect("localhost","root","root");
mysql_select_db("dbrelasi",$conn);
//tabel product
 $sql= "insert into product (nama, harga, jenis) values ('{$_POST['nama']} ',' {$_POST['harga']}',' {$_POST['jenis']}')";
 mysql_query($sql) or die ('gagal menyimpan product');
// mencari id product
$sql = "select max(id_product) as last_id from product limit 1";
$hasil= mysql_query($sql);
$row= mysql_fetch_array($hasil);
$last_id = $row['last_id'];
//menyimpan data buku ke tabel buku
$sql ="insert into buku (id_product,penulis,penerbit,isbn,tgl_terbit)
value ('$last_Id','{$_POST['penulis']}',' {$_POST['penerbit']}','{$_POST['isbn']}','{$_POST['tgl_terbit']}')";
mysql_query($sql) or die('Gagal menyimpan data buku');
echo "data tersimpan";

}
?>

<a href="tampil.php">Tampilkan Tabel</a>


 


Hasil dari simpan.php setelah di imput datanya , jika berhasil maka muncul seperti ini:





Lalu di klik Tampilkan Tabel muncul lagi tampilan seprti ini :


 




Oke  projectnya  selesai
------------------------------------------------------------o0o--------------------------------------------------
Dosen Pengampu = JOHN DHAINI (pengganti dosen utama)
Mata Kuliah      =  SISTEM BASIS DATA

------------------------------------------------------------o0o--------------------------------------------------