0% found this document useful (0 votes)
11 views

Praktik Membuat Create Read Update Delete

Uploaded by

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

Praktik Membuat Create Read Update Delete

Uploaded by

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

Membuat Create, Read, Update dan Delete

Menggunakan PHP dan MySQL

Langkah 1 Membuat database :

CREATE DATABASE barang_db;

USE barang_db;

CREATE TABLE 'barang'(


'id_barang' int(11) NOT NULL auto_increment,
'nama_barang' varchar(100) NOT NULL,
'harga_barang' int(11) NOT NULL,
'stok_barang' int(11) NOT NULL,
PRIMARY KEY ('id_barang')
);

Langkah 2 Membuat file PHP dengan nama index.php :

<html>
<head>
<title>Aplikasi CRUD Sederhana | Pemrograman
Web</title>
<style>
.table1 {
font-family: sans-serif;
color: #444;
border-collapse: collapse;
width: 50%;
border: 1px solid #f2f5f7;
}

.table1 tr th{
background: #964B00;
color: #fff;
font-weight: normal;
}

.table1, th, td {
padding: 8px 20px;
text-align: left;
}

.table1 tr:hover {
background-color: #f5f5f5;
}

.table1 tr:nth-child(even) {
background-color: #f2f2f2;
}
</style>
</head>
<body style="font-family:arial">
<center><h2>Aplikasi CRUD Sederhana <br />
Materi Pembelajaran : Pemrograman
Web</h2></center>

<hr />
<a href="tambah.php">+ Tambah Data Baru</a><br
/><br />
<b>Data Barang</b>
<table style="width:100%" class="table1">
<tr>
<th>No</th>
<th>Kode</th>
<th>Nama</th>
<th>Harga</th>
<th>Stok</th>
<th colspan=2><center>Opsi</center></th>
</tr>

<?php
include "koneksi.php";
$no = 1;
$data = mysqli_query($konek,"select * from
barang");
while($r = mysqli_fetch_array($data)){
$id_barang = $r['id_barang'];
$nama_barang = $r['nama_barang'];
$harga_barang = $r['harga_barang'];
$stok_barang = $r['stok_barang'];
?>
<tr><td><?php echo $no++; ?></td>
<td><?php echo $id_barang; ?></td>
<td><?php echo $nama_barang; ?></td>
<td><?php echo $harga_barang; ?></td>
<td><?php echo $stok_barang; ?></td>
<td align=right width=70px><a
href="edit.php?id=<?php echo
$id_barang;?>">Edit</a></td>
<td align=right width=70px><a
href="hapus.php?id=<?php echo
$id_barang;?>">Hapus</a></td>
</tr>
<?php
}
?>
</table>
</body>
</html>

Langkah 3 Membuat file PHP dengan nama edit.php :

<?php
// include database connection file
include "koneksi.php";

// Check if form is submitted for user update,


then redirect to homepage after update
if(isset($_POST['update']))
{
$id = $_POST['id'];

$nama_barang=$_POST['nama_barang'];
$harga_barang=$_POST['harga_barang'];
$stok_barang=$_POST['stok_barang'];
// update user data
$result = mysqli_query($konek, "UPDATE
barang SET
nama_barang='$nama_barang',harga_barang='$harga_
barang',stok_barang='$stok_barang' WHERE
id_barang=$id");

// Redirect to homepage to display updated


user in list
header("Location: index.php");
}
?>
<?php
// Display selected user data based on id
// Getting id from url
$id = $_GET['id'];

// Fetech user data based on id


$result = mysqli_query($konek, "SELECT * FROM
barang WHERE id_barang=$id");

while($r = mysqli_fetch_array($result))
{
$nama_barang = $r['nama_barang'];
$harga_barang = $r['harga_barang'];
$stok_barang = $r['stok_barang'];
}
?>

<html>
<head>
<title>Aplikasi CRUD Sederhana | Pemrograman
Web</title>
</head>
<body style="font-family:arial">
<center><h2>Aplikasi CRUD Sederhana <br />
Pemrograman Web</h2></center>
<hr />
<b>Edit Data Barang</b>
<br/><br/>
<form name="update_user" method="post"
action="edit.php">
<table border="0">
<tr>
<td>Nama Barang</td>
<td><input type="text" size="50"
name="nama_barang" value="<?php echo
$nama_barang;?>"></td>
</tr>
<tr>
<td>Harga Barang</td>
<td><input type="text" size="50"
name="harga_barang" value="<?php echo
$harga_barang;?>"></td>
</tr>
<tr>
<td>Stok Barang</td>
<td><input type="text" size="50"
name="stok_barang" value="<?php echo
$stok_barang;?>"></td>
</tr>
<tr>
<td><input type="hidden"
name="id" value=<?php echo $_GET['id'];?>></td>
<td><input type="submit"
name="update" value="Update"></td>
</tr>
</table>
</form>
</body>
</html>
Langkah 4 Membuat file PHP dengan nama koneksi.php :

<?php
// isi nama host, username mysql, dan password
mysql anda
$konek = mysqli_connect("localhost","root","");

// isikan dengan nama database yang akan di


hubungkan
$database = mysqli_select_db($konek,
"barang_db");
?>

Langkah 5 Membuat file PHP dengan nama tambah.php :

<html>
<head>
<title>Aplikasi CRUD Sederhana | Pemrograman
Web</title>
</head>
<body style="font-family:arial">
<center><h2>Aplikasi CRUD Sederhana <br />
Pemrograman Web</h2></center>
<hr />
<b>Tambah Data Baru</b>
<br/><br/>

<form action="tambah.php" method="post"


name="form1">
<table width="100%" border="0">
<tr>
<td>Nama Barang</td>
<td><input type="text"
name="nama_barang" size="50" required></td>
</tr>
<tr>
<td>Harga Barang</td>
<td><input type="text"
name="harga_barang" size="50" required></td>
</tr>
<tr>
<td>Stok Barang</td>
<td><input type="text"
name="stok_barang" size="50" required></td>
</tr>
<tr>
<td></td>
<td><input type="submit"
name="Submit" value="+ Tambahkan"></td>
</tr>
</table>
</form>

<?php

// Check If form submitted, insert form data


into users table.
if(isset($_POST['Submit'])) {
$nama_barang = $_POST['nama_barang'];
$harga_barang = $_POST['harga_barang'];
$stok_barang = $_POST['stok_barang'];

// include database connection file


include "koneksi.php";

// Insert user data into table


$tambah_barang = "insert into barang
values('','$nama_barang','$harga_barang','$stok_
barang')";
$kerjakan=mysqli_query($konek,
$tambah_barang);
if($kerjakan)
{
// Show message when user added
echo "Barang berhasil ditambahkan. <a
href='index.php'>Lihat Data Barang</a>";
}
else
{
echo "Gagal bro";
}
}
?>
</body>
</html>

You might also like