-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtambah-produk.php
187 lines (149 loc) · 6.11 KB
/
tambah-produk.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
session_start();
include 'db.php';
if($_SESSION['status_login'] != true){
echo '<script>window.location="login.php"</script>';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0" />
<title>Tambah Produk</title>
<!-- CSS -->
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link href="css/materialize.css" type="text/css" rel="stylesheet" media="screen,projection" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" />
<link href="css/style.css" type="text/css" rel="stylesheet" media="screen,projection" />
<script src="https://cdn.ckeditor.com/4.14.0/standard/ckeditor.js"></script>
</head>
<body>
<!--===== HEADER =====-->
<nav class="teal darken-2 lighten-1" role="navigation">
<div class="nav-wrapper container"><a id="logo-container" href="#" class="brand-logo">Dashboard</a>
<ul class="right hide-on-med-and-down">
<li><a href="dashboard.php">Dashboard</a></li>
<li><a href="profil.php">Profil</a></li>
<li><a href="data-kategori.php">Data Kategori</a></li>
<li><a href="data-produk.php">Data Produk</a></li>
<li><a href="keluar.php">Keluar</a></li>
</ul>
<ul id="nav-mobile" class="sidenav">
<li><a href="dashboard.php">Dashboard</a></li>
<li><a href="profil.php">Profil</a></li>
<li><a href="data-kategori.php">Data Kategori</a></li>
<li><a href="data-produk.php">Data Produk</a></li>
<li><a href="keluar.php">Keluar</a></li>
</ul>
<a href="#" data-target="nav-mobile" class="sidenav-trigger"><i class="material-icons">menu</i></a>
</div>
</nav>
<!-- content -->
<div class="section">
<div class="container">
<h3>Tambah Data Produk</h3>
<div class="box">
<form action="" method="POST" enctype="multipart/form-data">
<select name="kategori" required>
<option value="" >Pilih Kategori</option>
<?php
$kategori = mysqli_query($conn, "SELECT * FROM tb_kategori ORDER BY kategori_id DESC");
while($r = mysqli_fetch_array($kategori)){
?>
<option value="<?php echo $r['kategori_id'] ?>"><?php echo $r['kategori_nama'] ?></option>
<?php } ?>
</select>
<label>Pilih Kategori</label>
<input type="text" name="nama" class="input-control" placeholder="Nama Produk" required>
<input type="text" name="harga" class="input-control" placeholder="Harga" required>
<div class="file-field input-field">
<div class="btn">
<span>File</span>
<input type="file" multiple name="gambar" required>
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text" placeholder="Upload one or more files">
</div>
</div>
<textarea class="input-control" name="deskripsi" placeholder="Deskripsi"></textarea><br>
<select class="input-control" name="status">
<option value="">--Status--</option>
<option value="1">Aktif</option>
<option value="0">Tidak Aktif</option>
</select>
<button class="btn waves-effect waves-light" type="submit" name="submit">Submit
</button>
</form>
<?php
if(isset($_POST['submit'])){
// print_r($_FILES['gambar']);
// menampung inputan dari form
$kategori = $_POST['kategori'];
$nama = $_POST['nama'];
$harga = $_POST['harga'];
$deskripsi = $_POST['deskripsi'];
$status = $_POST['status'];
// menampung data file yang diupload
$filename = $_FILES['gambar']['name'];
$tmp_name = $_FILES['gambar']['tmp_name'];
$type1 = explode('.', $filename);
$type2 = $type1[1];
$newname = 'produk'.time().'.'.$type2;
// menampung data format file yang diizinkan
$tipe_diizinkan = array('jpg', 'jpeg', 'png', 'gif');
// validasi format file
if(!in_array($type2, $tipe_diizinkan)){
// jika format file tidak ada di dalam tipe diizinkan
echo '<script>alert("Format file tidak diizinkan")</script>';
}else{
// jika format file sesuai dengan yang ada di dalam array tipe diizinkan
// proses upload file sekaligus insert ke database
move_uploaded_file($tmp_name, './produk/'.$newname);
$insert = mysqli_query($conn, "INSERT INTO tb_produk VALUES (
null,
'".$kategori."',
'".$nama."',
'".$harga."',
'".$deskripsi."',
'".$newname."',
'".$status."',
null
) ");
if($insert){
echo '<script>alert("Tambah data berhasil")</script>';
echo '<script>window.location="data-produk.php"</script>';
}else{
echo 'gagal '.mysqli_error($conn);
}
}
}
?>
</div>
</div>
</div>
<!--===== FOOTER =====-->
<footer>
<div class="container">
<small>Copyright © 2022 <a class="teal-text text-darken-2 " href="https://www.himang.my.id/">Himang.my.id</a></small>
</div>
</footer>
<!-- Scripts-->
<script src="https://code.jquery.com/jquery-2.1.1.min.js "></script>
<script src="js/materialize.js "></script>
<script src="js/init.js "></script>
<script>
CKEDITOR.replace( 'deskripsi' );
</script>
<script>
document.addEventListener('DOMContentLoaded', function() {
var elems = document.querySelectorAll('select');
var instances = M.FormSelect.init(elems, options);
});
// Or with jQuery
$(document).ready(function(){
$('select').formSelect();
});
</script>
</body>
</html>