Postingan lainnya
Buku Ini Koding!
Baru!
Buku ini akan jadi teman perjalanan kamu belajar sampai dapat kerjaan di dunia programming!
Kelas Premium!
Belajar bikin website dari nol sekarang
Gunakan kupon "lebihcepat" untuk diskon 25%!
harga tdk bisa tersimpan ke database menggunakan OOP
<?php
class database{
var $host = "localhost";
var $uname = "root";
var $pass = "";
var $db = "tugas22518";
var $koneksi='';
function __construct(){
$this->koneksi = mysqli_connect($this->host, $this->uname, $this->pass);
mysqli_select_db($this->koneksi,$this->db);
}
//produk
function tampil_produk(){
$hasil=array();
$sql = mysqli_query($this->koneksi,"SELECT * FROM produk");
while($d = mysqli_fetch_array($sql)){
$hasil[] = $d;
}
return $hasil;
}
public function delete($table,$id)
{
$res = mysqlI_query($this->koneksi,"DELETE FROM $table WHERE id_produk=".$id);
return $res;
}
public function update($table,$id,$namaprod,$kateg,$gambar,$harga)
{
$res = mysqli_query($this->koneksi,"UPDATE $table SET nama_produk='$namaprod', id_kategori='$kateg', gambar='$gambar', harga='$harga' WHERE id_produk=".$id);
return $res;
}
public function insert($namaprod,$kateg,$gambar,$harga)
{
$res = mysqlI_query($this->koneksi,"INSERT produk(nama_produk,id_kategori,gambar,harga) VALUES('$namaprod','$kateg','$gambar','$harga')");
return $res;
}
}
?>
<?php
include '../controler/database.php';
$db = new database();
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
function del_id(id)
{
if(confirm('yakin dihapus?'))
{
window.location='../model/produk/hapus.php?delete_id='+id
}
}
function edit_id(id)
{
if(confirm('yakin di edit?'))
{
window.location='../model/produk/edit.php?edit_id='+id
}
}
</script>
</head>
<body>
<link rel="stylesheet" type="text/css" href="../bs/css/bootstrap.css">
<!-- bootstrap -->
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">Bukalapak KW 1000</a>
</div>
<ul class="nav navbar-nav">
<li class="active"><a href="../index.php">Home</a></li>
<li><a href="../view/index.php">Produk</a></li>
<li><a href="../view/indexkat.php">kategori</a></li>
</ul>
</div>
</nav>
<h1>DATA PRODUK</h1>
<a href="../model/produk/tambah.php"><button>tambah</button></a>
<a href="../index.php"><button>menu utama</button></a>
<table style="right: 0" border="1" align="center" cellpadding="0" cellspacing="0">
<tr>
<th width="1%"><center>no</center></th>
<th width="1%"><center>id </center></th>
<th width="1%"><center>nama produk</center></th>
<th width="1%"><center>kategori</center></th>
<th width="1%"><center>gambar</center></th>
<th width="1%"><center>Harga</center></th>
<th width="1%"><center>edit</center></th>
<th width="1%"><center>hapus</center></th>
</tr>
<?php
$no=1;
foreach($db->tampil_produk() as $x){
?>
<tr>
<td><?php echo $no++; ?></td>
<td><?php echo $x['id_produk'];?></td>
<td><?php echo $x['nama_produk'];?></td>
<td><?php echo $x['id_kategori'];?></td>
<td><img src="../img/<?php echo $x['gambar'];?>"></td>
<td><?php echo $x['harga'];?></td>
<td><a href="javascript:edit_id(<?php echo$x['id_produk']; ?>)">edit</a></td>
<td><a href="javascript:del_id(<?php echo$x['id_produk']; ?>)">hapus</a></td>
</tr>
<?php
}
?>
</table>
<script src="../bs/js/bootstrap.js"></script>
</body>
</html>
<?php
include_once '../../controler/database.php';
$con = new database();
// data insert code starts here.
if(isset($_POST['btn-save']))
{
$namaprod = $_POST['namaproduk'];
$kateg = $_POST['kategori'];
$gambar = $_POST['gambar'];
$harga + $_POST['harga'];
$res=$con->insert($namaprod,$kateg,$gambar,$harga);
if($res)
{
?>
<script>
alert('data berhasil ditambahkan');
window.location='../../view/index.php'
</script>
<?php
}
else
{
?>
<script>
alert('data gagal ditambahkan');
window.location='../../view/index.php'
</script>
<?php
}
}
// data insert code ends here.
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>tambah produk</title>
<!-- <link rel="stylesheet" href="style.css" type="text/css" /> -->
</head>
<body>
<center>
<div id="header">
<div id="content">
<label>tambah produk</label>
</div>
</div>
<div id="body">
<div id="content">
<form method="post">
<table align="center">
<tr>
<td><input type="text" name="namaproduk" placeholder="nama produk" required /></td>
</tr>
<tr>
<td><input type="number" name="kategori" placeholder="kategori" required /></td>
</tr>
<tr>
<td><input type="file" name="gambar" placeholder="gambar" required /></td>
</tr>
<tr>
<td><input type="text" name="harga" placeholder="harga" required /></td>
</tr>
<tr>
<td>
<button type="submit" name="btn-save"><strong>SAVE</strong></button></td>
</tr>
</table>
</form>
</div>
</div>
</center>
</body>
</html>
0
Belum ada Jawaban. Jadi yang pertama Jawaban
Login untuk ikut Jawaban