Blogger Themes

Jumat, 11 Mei 2012

Input Data & Hasil Dalam Satu Halaman

Halloo sobat blogger.

Baru posting artikel lagi ni kemaren sibuk dengan urusan kerja dan tugas kampus. Dapet request dari temen buat CRUD  dengan 1 halaman.CRUD merupakan singkatan dari Create Read  Update Delete yang artinya membuat membaca pembaruan menghapus. Banyak bahasa program yang bisa CRUD salah satunya seperti PHP , VB ASP.NET dan masi banyak lagi bahasa program yg kita ingin membuat CRUD. Disini saya membuat CRUD dengan bahasa php dan view table data menggunakan javascript. Database yang digunakan mysql atau sql server dll, dan disini saya menggunakan mysql . Oke . . langsung saja kita praktekan

Langkah pertama kita buatkan dahulu database dengan nama hadiat dan table produk atau terserah sesuai kebutahan nama database anda.

Table produk
`id` CHAR (5) NOT NULL
`Nama` VARCHAR (25) NOT NULL
`Jenis_Produk` VARCHAR (50) NOT NULL
`Stock_Barang` CHAR (5) NOT NULL
`Harga` INT (10) NOT NULL
`Harga_Jual` INT (10)NOT NULL
PRYMARY KEY (`id`)

Buat file di server local kita dengan nama hadiat, lalu kita buat koneksi database buka text editor anda ketikan scipt dibawah ini.

koneksi.php
<?php
    $host = "localhost";
    $username = "root";
    $password = "";
    $database = "hadiat";
    if (!$dbh=mysql_connect($host,$username,$password))
    {
        echo mysql_error();
        exit;
    }
    mysql_select_db($database,$dbh);
?>

buat form atau tampilan untuk input data ke database 

index.php

<?php
include "koneksi.php";
?>
<html>
    <head>

      
        <title>Input & Edit Satu Halaman</title>      
        <link rel="stylesheet" href="style.css" type="text/css"/>
        <script type="text/javascript" src="include/jquery-latest.js"></script>
        <script type="text/javascript" src="include/jquery.tablesorter.js"></script>
        <script type="text/javascript">
        $(function() {
            $("table").tablesorter({debug: true});
        });
      
        </script>
      
        <style type="text/css">
        body {font-size: 20px;}
      
        #input {
        border : 1px solid black;
        border-radius : 10px;
        height: 30px;
        width: 500px;
        }
      
        td { padding: 5px; }
        </style>      
      
    </head>
   
    <body>
        <h2 style="color:blue;">Data Input & Hasil Dalam Satu Halaman</h2>
    <form action="aksi.php" method="post">
    <table>
      
        <tr>
            <td>Id_Produk</td>
            <td>:</td>
            <td><input type="text" id="input" name="id" ></td>
        </tr>
      
        <tr>
            <td>Nama</td>
            <td>:</td>
            <td><input type="text" id="input" name="name" ></td>
        </tr>      
      
        <tr>
            <td>Jenis Produk</td>
            <td>:</td>
            <td><input type="text" id="input" name="jp" ></td>
        </tr>
      
        <tr>
            <td>Stock Barang</td>
            <td>:</td>
            <td><input type="text" id="input" name="sb" ></td>
        </tr>
      
        <tr>
            <td>Harga</td>
            <td>:</td>
            <td><input type="text" id="input" name="harga" ></td>
        </tr>
      
        <tr>
            <td>Harga Jual</td>
            <td>:</td>
            <td><input type="text" id="input" name="hj" ></td>
        </tr>      
      
        <tr>
          
            <td> <input type="submit" value="Simpan"> </td>
            <td> <input type="reset" value="Batal"> </td>  
    </table>
    </form>
   
    <table id="rowspan" cellspacing="0" class="tablesorter">
    <thead>
          
        <tr>
            <th width="100px" style="color: blue">Id Produk</th>
            <th style="color: blue">Nama</th>
            <th style="color: blue" width="400px" >Jenis Produk</th>
            <th width="100px" style="color: blue">Stock Produk</th>
            <th style="color: blue" >Harga</th>
            <th style="color: blue" >Harga Jual</th>
            <th style="color: blue" width="100px">Operator</th>
          
        </tr>
    </thead>
    <tbody>
   
        <?php
        // Nampilin Data
      
      
        $query = mysql_query("select * from produk");

        while ($r= mysql_fetch_array($query)){
        echo "<tr>
            <td style=\"color: black\">$r[id]</td>
            <td style=\"color: black\">$r[Nama]</td>
            <td style=\"color: black\" >$r[Jenis_Produk]</td>
            <td style=\"color: black\">$r[Stock_Barang]</td>
            <td style=\"color: black\">$r[Harga]</td>
            <td style=\"color: black\">$r[Harga_Jual]</td>
            <td >
          
            <a href=\"edit.php?id=$r[id]\" OnClick='window.open ('edit.php?id=$r[id]'\")\' style=\"color: black\"><img src=\"edit.png\" title='edit'></img>
            <a href='hapus.php?id=$r[id]' onclick = \"return confirm ('apakah anda yakin ingin menghapusnya?')\"><img src=\"delete.png\" title='delete'></img></td>
          
        </tr>";
   
        }
        ?>      
      
    </tbody>
</table>
    </body>
</html>

Buat aksi dari form tampilan untuk masukan data ke database

aksi.php 

<?php
include "koneksi.php";
// insert data

$id     = $_POST['id'];
$name    = $_POST['name'];            
$jp        = $_POST['jp'];
$sb        = $_POST['sb'];
$harga    = $_POST['harga'];
$hj        = $_POST['hj'];

if (empty($id)){
    echo "ID belum diisi";
    echo "<br/>";
}

if (empty($name)){
    echo "Name belum diisi";
    echo "<br/>";
}

if (empty($jp)){
    echo "Jenis Produk belum diisi";
    echo "<br/>";
}

if (empty($sb)){
    echo "Stock Barang belum diisi";
    echo "<br/>";
}

if (empty($harga)){
    echo "Harga belum diisi";
    echo "<br/>";
}

if (empty($hj)){
    echo "Harga Jual belum diisi";
    echo "<br/>";
    echo "<blink> <a href='index.php' style='color:black'> Kembali </a> </blink>";
}else {

$query = "INSERT INTO produk (id,Nama, Jenis_Produk, Stock_Barang, Harga, Harga_Jual)
        values ('$id','$name', '$jp', '$sb','$harga', '$hj' )";

$kon = mysql_query($query);

?><script language="javascript">window.location.href="index.php"</script><?

}
?>

Buat form edit untuk edit data

edit.php
 <?php
include "koneksi.php";
?>
<html>
    <head>
       
   
        <style type="text/css">
        #input {
        border : 1px solid black;
        border-radius : 10px;
        height: 30px;
        width: 500px;
        }
       
        </style>
    </head>
   
    <body>
        <h2 style="color:blue;">Data Input & Hasil Dalam Satu Halaman</h2>
    <form action="act_edit.php" method="post">
   
    <?php
           
        $sql    = "SELECT * FROM produk WHERE id = '$_GET[id]' ";
        $con    = mysql_query($sql);
        $r        = mysql_fetch_array($con);
        ?>
    <table>
       
       
            <input type="hidden" id="input" name="id" value="<?php echo $r[id];?>">
       
       
        <tr>
            <td>Nama</td>
            <td>:</td>
            <td><input type="text" id="input" name="name" value="<?php echo $r[Nama];?>"></td>
        </tr>
       
       
        <tr>
            <td>Jenis Produk</td>
            <td>:</td>
            <td><input type="text" id="input" name="jp" value="<?php echo $r[Jenis_Produk];?>"></td>
        </tr>
       
        <tr>
            <td>Stock Barang</td>
            <td>:</td>
            <td><input type="text" id="input" name="sb" value="<?php echo $r[Stock_Barang];?>"></td>
        </tr>
       
        <tr>
            <td>Harga</td>
            <td>:</td>
            <td><input type="text" id="input" name="harga" value="<?php echo $r[Harga];?>"></td>
        </tr>
       
        <tr>
            <td>Harga Jual</td>
            <td>:</td>
            <td><input type="text" id="input" name="hj" value="<?php echo $r[Harga_Jual];?>"></td>
        </tr>
       
       
        <tr>
           
            <td> <input type="submit" value="Simpan"> </td>
            <td> <input type="reset" value="Batal"> </td>   
    </table>
    </form> 

Buat aksi edit untuk masukan data ke database

act_edit.php
<?php
include "koneksi.php";

$id     = $_POST['id'];
$name    = $_POST['name'];           
$jp        = $_POST['jp'];
$sb        = $_POST['sb'];
$harga    = $_POST['harga'];
$hj        = $_POST['hj'];

$kon =("UPDATE produk set `Nama`= '$name', `Jenis_Produk`= '$jp', `Harga`= '$harga', `Harga_Jual`= '$hj' where id='$id'");
$myq = mysql_query ($kon);

?><script type="text/javascript">window.location.href="index.php"</script><?;

?>

Dan terakhir buat file hapus untuk menghapus satu data

hapus.php
<?php
include "koneksi.php";

$query = "DELETE FROM produk WHERE id='$_GET[id]'";
$jalan = mysql_query($query);

?><script type="text/javascript">window.location.href="index.php"</script><?;

?>

Selesai dah artikel Input Data & Hasil Dalam Satu Halaman sekarang coba jalankan aplikasinya di browser kesayangan anda ketikan localhost/hadiat/index.php maka akan tampil sebagai berikut

Naaaah untuk yang ga mau cape ketik - ketik mau yang langsung pake silahkan didownload di sini.
semoga membantu anda







Ditulis Oleh : Radi Wau // 14.05
Kategori:

5 komentar:

  1. Komentar ini telah dihapus oleh administrator blog.

    BalasHapus
  2. Komentar ini telah dihapus oleh pengarang.

    BalasHapus
  3. bro, sorry y klo w kritik. Mash byk yg slh tuh, posting ulang deh ilmu ente tuh, klo ud bs running bru bagi ilmunya lgi.

    BalasHapus
  4. Itu bukan satu halaman, tapi banyak halaman ( konek.php,edit.php dll)
    Ini referensi Crud Dalam satu halaman silahkan masukDisini Gan

    BalasHapus

 
Diberdayakan oleh Blogger.