0% found this document useful (0 votes)
4 views13 pages

CRUD Web App for Product Management

The document outlines a web programming project by Cindy Setyowati, detailing a CRUD application for managing products. It includes PHP code for connecting to a MySQL database, displaying products, adding, editing, and deleting product entries. The document also contains HTML and CSS for the user interface, along with necessary form handling and validation logic.

Uploaded by

cindyssetyo
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)
4 views13 pages

CRUD Web App for Product Management

The document outlines a web programming project by Cindy Setyowati, detailing a CRUD application for managing products. It includes PHP code for connecting to a MySQL database, displaying products, adding, editing, and deleting product entries. The document also contains HTML and CSS for the user interface, along with necessary form handling and validation logic.

Uploaded by

cindyssetyo
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

Nama : Cindy Setyowati

NIM : 312210693
Kelas : TI 22 C8
Mata Kuliah : Pemrograman Web
Dosen Pengampu : Sophian Andhika Sardi, [Link]., [Link].
Output :
Halaman Awal :

Tampilan Button Tambah Produk :


Tampilan Button Edit :

Notifikasi Hapus :
Phpmyadmin:

Source Code :

[Link]
<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "latihan_crud";
$koneksi = mysqli_connect($host,$user,$pass,$db);

if (!$koneksi) {
die("Koneksi dengan database gagal : ".mysqli_connect_error());
}
?>

[Link] :
<?php include('[Link]'); ?>
<!DOCTYPE html>
<html>
<head>
<title>Cindy Crud</title>
</head>
<body>
<center><h1>Cindy Phone</h1></center>
<style type="text/css">
* {
font-family: "Trebuchet MS";
}
h1 {
text-transform: uppercase;
color: salmon;
}
table {
border: solid 1px #DDEEEE;
border-collapse: collapse;
border-spacing: 0;
width: 70%;
margin: 10px auto 10px auto;
}
table thead th {
background-color: #DDEFEF;
border: solid 1px #DDEEEE;
color: #336B6B;
padding: 10px;
text-align: left;
text-shadow: 1px 1px 1px #fff;
text-decoration: none;
}
table tbody td {
border: solid 1px #DDEEEE;
color: #333;
padding: 10px;
text-shadow: 1px 1px 1px #fff;
}
a {
background-color: salmon;
color: #fff;
padding: 10px;
text-decoration: none;
font-size: 12px;
}
</style>
<center><a href="tambah_produk.php">+ &nbsp; Tambah Produk</a></center>
<table>
<thead>
<tr>
<th>No.</th>
<th>Gambar</th>
<th>Nama Barang</th>
<th>Kategori</th>
<th>Harga Jual</th>
<th>Harga Beli</th>
<th>Stok</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT * FROM data_barang ORDER BY id_barang ASC";
$result = mysqli_query($koneksi,$query);

if(!$result) {
die("Query Error
:".mysqli_errno($koneksi)."".mysqli_error($koneksi));
}
$no = 1;
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php echo $no; ?></td>
<td><img style="width: 120px;" src="gambar/<?php echo
$row['gambar']; ?>"></td>
<td><?php echo $row['nama_barang']; ?></td>
<td><?php echo $row['kategori']; ?></td>
<td>Rp <?php echo number_format($row['harga_jual'],0,',','.');
?></td>
<td>Rp <?php echo number_format($row['harga_beli'],0,',','.');
?></td>
<td><?php echo $row['stock']; ?></td>
<td>
<a href="edit_produk.php?id=<?php echo $row['id_barang'];
?>">Edit</a> |
<a href="proses_hapus.php?id=<?php echo $row['id_barang']; ?>"
onclick="return confirm('Anda yakin akan menghapus data ini?')">Hapus</a>
</td>
</tr>
<?php
$no++;
}
?>
</tbody>

</table>
</body>
</html>

Tambah_produk :
<?php include('[Link]'); ?>
<!DOCTYPE html>
<html>
<head>
<title>Cindy CRUD</title>
<style type="text/css">
* {
font-family: "Trebuchet MS";
}
h1 {
text-transform: uppercase;
color: salmon;
}
button {
background-color: salmon;
color: #fff;
padding: 10px;
text-decoration: none;
font-size: 12px;
border: 0px;
margin-top: 20px;
}
label {
margin-top: 10px;
float: left;
text-align: left;
width: 100%;
}
input {
padding: 6px;
width: 100%;
box-sizing: border-box;
background: #f8f8f8;
border: 2px solid #ccc;
outline-color: salmon;
}
div {
width: 100%;
height: auto;
}
.base {
width: 400px;
height: auto;
padding: 20px;
margin-left: auto;
margin-right: auto;
background: #ededed;
}
</style>
</head>
<body>
<center><h1>Tambah Produk</h1></center>
<form method="POST" action="proses_tambah.php" enctype="multipart/form-
data">
<section class="base">
<div>
<label>Gambar</label>
<input type="file" name="gambar" autofocus="" required="" />
</div>
<div>
<label>Nama Produk</label>
<input type="text" name="nama_barang" autofocus="" required="" />
</div>
<div>
<label>Kategori</label>
<input type="text" name="kategori" autofocus="" required="" />
</div>
<div>
<label>Harga Jual</label>
<input type="text" name="harga_jual" autofocus="" required="" />
</div>
<div>
<label>harga_beli</label>
<input type="text" name="harga_beli" autofocus="" required="" />
</div>
<div>
<label>Stock</label>
<input type="text" name="stock" autofocus="" required="" />
</div>
<div>
<button type="submit">Simpan Produk</button>
</div>
</section>

Proses_tambah :
<?php
include '[Link]';

$gambar = $_FILES['gambar']['name'];
$nama_barang = $_POST['nama_barang'];
$kategori = $_POST['kategori'];
$harga_jual = $_POST['harga_jual'];
$harga_beli = $_POST['harga_beli'];
$stock = $_POST['stock'];

if($gambar != "") {
$ekstensi_diperbolehkan = array('png','jpg', 'jpeg');
$x = explode('.', $gambar);
$ekstensi = strtolower(end($x));
$file_tmp = $_FILES['gambar']['tmp_name'];
$angka_acak = rand(1,999);
$nama_gambar_baru = $angka_acak.'-'.$gambar;
if(in_array($ekstensi, $ekstensi_diperbolehkan) === true) {
move_uploaded_file($file_tmp, 'gambar/'.$nama_gambar_baru);
$query = "INSERT INTO data_barang (gambar, nama_barang,
kategori, harga_jual, harga_beli, stock ) VALUES
('$nama_gambar_baru','$nama_barang', '$kategori', '$harga_jual',
'$harga_beli','$stock' )";
$result = mysqli_query($koneksi, $query);

if(!$result){
die ("Query gagal dijalankan: ".mysqli_errno($koneksi).
" - ".mysqli_error($koneksi));
} else {

echo "<script>alert('Data berhasil


ditambah.');[Link]='[Link]';</script>";
}

} else {

echo "<script>alert('Ekstensi gambar yang boleh hanya jpg atau


png.');[Link]='tambah_produk.php';</script>";
}
} else {
$query = "INSERT INTO data_barang (gambar, nama_barang, kategori, harga_jual,
harga_beli, stock ) VALUES ('$nama_gambar_baru','$nama_barang', '$kategori',
'$harga_jual', '$harga_beli','$stock', null)";
$result = mysqli_query($koneksi, $query);
if(!$result){
die ("Query gagal dijalankan: ".mysqli_errno($koneksi).
" - ".mysqli_error($koneksi));
} else {

echo "<script>alert('Data berhasil


ditambah.');[Link]='[Link]';</script>";
}
}

Edit_produk :
<?php
include ('[Link]');

if (isset($_GET['id'])) {
$id = ($_GET["id"]);

$query = "SELECT * FROM data_barang WHERE id_barang='$id'";


$result = mysqli_query($koneksi, $query);
if(!$result){
die ("Query Error: ".mysqli_errno($koneksi).
" - ".mysqli_error($koneksi));
}
$data = mysqli_fetch_assoc($result);
if (!count($data)) {
echo "<script>alert('Data tidak ditemukan pada
database');[Link]='[Link]';</script>";
}
} else {

echo "<script>alert('Masukkan data


id.');[Link]='[Link]';</script>";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>edit barang</title>
<style type="text/css">
* {
font-family: "Trebuchet MS";
}
h1 {
text-transform: uppercase;
color: salmon;
}
button {
background-color: salmon;
color: #fff;
padding: 10px;
text-decoration: none;
font-size: 12px;
border: 0px;
margin-top: 20px;
}
label {
margin-top: 10px;
float: left;
text-align: left;
width: 100%;
}
input {
padding: 6px;
width: 100%;
box-sizing: border-box;
background: #f8f8f8;
border: 2px solid #ccc;
outline-color: salmon;
}
div {
width: 100%;
height: auto;
}
.base {
width: 400px;
height: auto;
padding: 20px;
margin-left: auto;
margin-right: auto;
background: #ededed;
}
</style>
</head>
<body>
<center>
<h1>Edit Produk <?php echo $data['nama_barang']; ?></h1>
<center>
<form method="POST" action="proses_edit.php" enctype="multipart/form-data"
>
<input name="id_barang" value="<?php echo $data['id_barang']; ?>" hidden
/>
<section class="base">

<div>
<label>Gambar Produk</label>
<img src="gambar/<?php echo $data['gambar']; ?>" style="width:
120px;float: left;margin-bottom: 5px;">
<input type="file" name="gambar" />
<i style="float: left;font-size: 11px;color: red">Abaikan jika tidak
merubah gambar produk</i>
</div>
<div>
<label>Nama Produk</label>
<input type="text" name="nama_barang" value="<?php echo
$data['nama_barang']; ?>" />
</div>
<div>
<label>Harga Jual</label>
<input type="text" name="harga_jual" required=""value="<?php echo
$data['harga_jual']; ?>" />
</div>
<div>
<label>Harga Beli</label>
<input type="text" name="harga_beli" required="" value="<?php echo
$data['harga_beli']; ?>"/>
</div>
<div>
<label>Stock</label>
<input type="text" name="stock" required="" value="<?php echo
$data['stock']; ?>"/>
</div>
<div>
<button type="submit">Simpan Perubahan</button>
</div>
</section>
</form>
</body>
</html>

Proses_edit
<?php
include '[Link]';

$id = $_POST['id'];
$gambar = $_FILES['gambar']['name'];
$nama_barang = $_POST['nama_barang'];
$kategori = $_POST['kategori'];
$harga_jual = $_POST['harga_jual'];
$harga_beli = $_POST['harga_beli'];
$stock = $_POST['stock'];
if($gambar != "") {
$ekstensi_diperbolehkan = array('png','jpg', 'jpeg');
$x = explode('.', $gambar);
$ekstensi = strtolower(end($x));
$file_tmp = $_FILES['gambar']['tmp_name'];
$angka_acak = rand(1,999);
$nama_gambar_baru = $angka_acak.'-'.$gambar;
if(in_array($ekstensi, $ekstensi_diperbolehkan) === true) {
move_uploaded_file($file_tmp, 'gambar/'.$nama_gambar_baru);
$query = "UPDATE data_barang SET gambar = '$nama_gambar_baru',
nama_barang = '$nama_barang', kategori = '$kategori', harga_jual = '$harga_jual',
harga_beli = '$harga_beli', stock = '$stock'";
$query .= "WHERE id_barang = '$id'";
$result = mysqli_query($koneksi, $query);

if(!$result){
die ("Query gagal dijalankan: ".mysqli_errno($koneksi).
" - ".mysqli_error($koneksi));
} else {

echo "<script>alert('Data berhasil


diubah.');[Link]='[Link]';</script>";
}

} else {

echo "<script>alert('Ekstensi gambar yang boleh hanya jpg atau


png.');[Link]='edit_produk.php';</script>";
}
} else {
$ $query = "UPDATE data_barang SET nama_barang = '$nama_barang', kategori =
'$kategori', harga_jual = '$harga_jual', harga_beli = '$harga_beli', stock =
'$stock'";
$query .= "WHERE id_barang = '$id'";
$result = mysqli_query($koneksi, $query);

if(!$result){
die ("Query gagal dijalankan: ".mysqli_errno($koneksi).
" - ".mysqli_error($koneksi));
} else {
echo "<script>alert('Data berhasil
diubah.');[Link]='[Link]';</script>";
}
}

Hapus_produk
<?php
include '[Link]';
$id = $_GET["id"];

$query = "DELETE FROM data_barang WHERE id_barang='$id' ";


$hasil_query = mysqli_query($koneksi, $query);
if(!$hasil_query) {
die ("Gagal menghapus data: ".mysqli_errno($koneksi).
" - ".mysqli_error($koneksi));
} else {
echo "<script>alert('Data berhasil
dihapus.');[Link]='[Link]';</script>";
}

You might also like