index.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>TOKO</title>
<?php include "koneksi_db.php";?>
</head>
<body>
<table width="100%" border="1">
<tr bgcolor="#666666" style="color:#FFF; ">
<td align="center">No</td>
<td align="center">ID Barang</td>
<td align="center">Nama Barang</td>
<td align="center">Stok Barang</td>
<td align="center">Aksi</td>
</tr>
<!-- INI READ -->
<?php
$no=1;
$e = mysql_query("SELECT * FROM barang");
while($data = mysql_fetch_array($e)){
echo "
<tr>
<td align=center>$no</td>
<td align=center>$data[id_barang]</td>
<td>$data[nama_barang]</td>
<td align=right>$data[stok]</td>
<td align=center>
<a href=form_edit.php?id_barang=$data[id_barang]>Edit</a> |
<a href=proses.php?act=hapus&id_barang=$data[id_barang]>Delete</a>
</td>
</tr>
";
$no++;
}
?>
</table>
<br/>
<a href="form_tambah.php">Tambah data</a>
</body>
</html>
form_tambah.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<form action="proses.php?act=tambah_barang" method="post">
Nama Barang<input type="text" name="nama_barang"><br/>
Stok<input type="text" name="stok"><br/>
<input type="submit" value="SImpan">
</form>
</body>
</html>
form_edit.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<?php
include "koneksi_db.php";
$id_barang = $_GET['id_barang'];
#READ
$selek = mysql_query("SELECT * FROM barang WHERE id_barang = '$id_barang'") or die(mysql_error());
$f = mysql_fetch_array($selek);
?>
<form action="proses.php?act=edit_barang&id_barang=<?php echo $id_barang;?>" method="post">
Nama Barang<input type="text" name="nama_barang" value="<?php echo $f['nama_barang']?>"><br/>
Stok<input type="text" name="stok" value="<?php echo $f['stok']?>"><br/>
<input type="submit" value="SImpan">
</form>
</body>
</html>
koneksi_db.php
<?php
$host = "localhost";
$user = "root";
$pass = "";
$namadb = "toko";
mysql_connect($host, $user, $pass);
mysql_select_db($namadb);
?>
proses.php
<?php
include "koneksi_db.php";
$act = $_GET['act'];
#INSERT TO DATABASE
if($act=="tambah_barang"){
$nama_barang = $_POST['nama_barang'];
$stok = $_POST['stok'];
mysql_query("INSERT INTO barang(id_barang, nama_barang, stok) VALUES(null, '$nama_barang', '$stok')");
header('Location:index.php');
}
#UPDATE DATA
if($act=="edit_barang"){
$nama_barang = $_POST['nama_barang'];
$stok = $_POST['stok'];
$id_barang = $_GET['id_barang'];
mysql_query("UPDATE BARANG SET nama_barang = '$nama_barang', stok = '$stok' WHERE id_barang = '$id_barang'");
header('Location:index.php');
}
#HAPUS
if($act=="hapus"){
$id_barang = $_GET['id_barang'];
mysql_query("DELETE FROM barang WHERE id_barang = '$id_barang'");
header('Location:index.php');
}
?>
Database
-- phpMyAdmin SQL Dump
-- version 3.3.9
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Mar 30, 2016 at 10:14 AM
-- Server version: 5.5.8
-- PHP Version: 5.3.5
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `toko`
--
-- --------------------------------------------------------
--
-- Table structure for table `barang`
--
CREATE TABLE IF NOT EXISTS `barang` (
`id_barang` int(2) unsigned zerofill NOT NULL AUTO_INCREMENT,
`nama_barang` varchar(100) NOT NULL,
`stok` int(5) NOT NULL,
PRIMARY KEY (`id_barang`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
--
-- Dumping data for table `barang`
--
INSERT INTO `barang` (`id_barang`, `nama_barang`, `stok`) VALUES
(01, 'Flashdisk Kingston 2GB', 10),
(02, 'Power supply', 17),
(05, 'hardisk', 17);
Catatan
#CREATE DATABASE
1.
Akses alamat
localhost/phpmyadmin menggunakan browser kesayangan antum eh anda maksudnya :D.
Pastikan service apache dan mysql nya udah jalan.
2.
Create DB dengan nama toko
3.
Import DB yang ada di folder
db. Nama db nya : toko.sql
#CREATE PHP CRUD
1.
Paste folder toko ke folder
htdocs.
2.
Akses alamat localhost/toko dan
enter.
3.
Perhatikan file koneksi_db.php.
itu script konek tu database
0 Comments