forked from kokonior/PHP-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5.php
240 lines (199 loc) · 6.69 KB
/
5.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<!DOCTYPE html>
<html>
<head>
<title>CRUD Petani Kode</title>
<link rel="icon" href="http://www.petanikode.com/favicon.ico" />
<meta charset="utf-8">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto|Varela+Round">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>
<body>
<style>
body {
color:black;
font-family: cursive;
font-size:20px;
background-image: url('bg/bg.jpg');
background-size:cover;
text-align: center;
}
fieldset legend h2{
font-size: 40px;
}
label input{
border-radius: 25px;
}
</style>
<?php
// --- koneksi ke database
$koneksi = mysqli_connect("localhost","root","","potomupotoku") or die(mysqli_error());
// --- Fngsi tambah data (Create)
function tambah($koneksi){
if (isset($_POST['btn_simpan'])){
$id = time();
$judul= $_POST['judul_foto'];
$caption = $_POST['caption'];
$foto = $_POST['foto'];
if(!empty($judul) && !empty($caption) && !empty($foto)){
$sql = "INSERT INTO poto (id,judul_foto,caption,foto) VALUES(".$id.",'".$judul."','".$caption."','".$foto."')";
$simpan = mysqli_query($koneksi, $sql);
if($simpan && isset($_GET['aksi'])){
if($_GET['aksi'] == 'create'){
header('location: index.php');
}
}
} else {
$pesan = "Tidak dapat menyimpan, data belum lengkap!";
}
}
?>
<form action="" method="POST">
<fieldset>
<legend><h2><br>POSTING FOTO</h2></legend>
<label>Judul Foto <input type="text" name="judul_foto" /></label> <br>
<label>Caption <input type="text" name="caption" /></label> <br>
<label>Foto <input type="file" name="foto" /></label> <br>
<br>
<label>
<input type="submit" name="btn_simpan" value="Upload"/>
<input type="reset" href="dashboard.html" value="Besihkan"/>
</label>
<li class="nav-item active">
<a class="btn" href="dashboard.html">
<span>Kembali</span></a>
</li>
<br>
<p><?php echo isset($pesan) ? $pesan : "" ?></p>
</fieldset>
</form>
<?php
}
// --- Tutup Fngsi tambah data
// --- Fungsi Baca Data (Read)
function tampil_data($koneksi){
$sql = "SELECT * FROM poto";
$query = mysqli_query($koneksi, $sql);
echo "<fieldset>";
echo "<legend><h2>POTO</h2></legend>";
echo "<table border='5' cellpadding='10' table align:center >";
echo "<tr>
<th>ID</th>
<th>Judul foto</th>
<th>caption/th>
<th>foto</th>
</tr>";
while($data = mysqli_fetch_array($query)){
?>
<tr>
<td><?php echo $data['id']; ?></td>
<td><?php echo $data['judul_foto']; ?></td>
<td><?php echo $data['caption']; ?> Kg</td>
<td><?php echo $data['foto']; ?> bulan</td>
<td>
<a href="index.php?aksi=update&id=<?php echo $data['id']; ?>&judul=<?php echo $data['nama_tanaman']; ?>&hasil=<?php echo $data['hasil_panen']; ?>&lama=<?php echo $data['lama_tanam']; ?>&tanggal=<?php echo $data['tanggal_panen']; ?>">Ubah</a> |
<a href="index.php?aksi=delete&id=<?php echo $data['id']; ?>">Hapus</a>
</td>
</tr>
<?php
}
echo "</table>";
echo "</fieldset>";
}
// --- Tutup Fungsi Baca Data (Read)
// --- Fungsi Ubah Data (Update)
function ubah($koneksi){
// ubah data
if(isset($_POST['btn_ubah'])){
$id = $_POST['id'];
$nm_tanaman = $_POST['nm_tanaman'];
$hasil = $_POST['hasil'];
$lama = $_POST['lama'];
$tgl_panen = $_POST['tgl_panen'];
if(!empty($nm_tanaman) && !empty($hasil) && !empty($lama) && !empty($tgl_panen)){
$perubahan = "nama_tanaman='".$nm_tanaman."',hasil_panen=".$hasil.",lama_tanam=".$lama.",tanggal_panen='".$tgl_panen."'";
$sql_update = "UPDATE tabel_panen SET ".$perubahan." WHERE id=$id";
$update = mysqli_query($koneksi, $sql_update);
if($update && isset($_GET['aksi'])){
if($_GET['aksi'] == 'update'){
header('location: index.php');
}
}
} else {
$pesan = "Data tidak lengkap!";
}
}
// tampilkan form ubah
if(isset($_GET['id'])){
?>
<a href="index.php"> « Home</a> |
<a href="index.php?aksi=create"> (+) Tambah Data</a>
<hr>
<form action="" method="POST">
<fieldset>
<legend><h2>Ubah data</h2></legend>
<input type="hidden" name="id" value="<?php echo $_GET['id'] ?>"/>
<label>Nama tanaman <input type="text" name="nm_tanaman" value="<?php echo $_GET['nama'] ?>"/></label> <br>
<label>Hasil panen <input type="number" name="hasil" value="<?php echo $_GET['hasil'] ?>"/> kg</label><br>
<label>Lama tanam <input type="number" name="lama" value="<?php echo $_GET['lama'] ?>"/> bulan</label> <br>
<label>Tanggal panen <input type="date" name="tgl_panen" value="<?php echo $_GET['tanggal'] ?>"/></label> <br>
<br>
<label>
<input type="submit" name="btn_ubah" value="Simpan Perubahan"/> atau <a href="index.php?aksi=delete&id=<?php echo $_GET['id'] ?>"> (x) Hapus data ini</a>!
</label>
<br>
<p><?php echo isset($pesan) ? $pesan : "" ?></p>
</fieldset>
</form>
<?php
}
}
// --- Tutup Fungsi Update
// --- Fungsi Delete
function hapus($koneksi){
if(isset($_GET['id']) && isset($_GET['aksi'])){
$id = $_GET['id'];
$sql_hapus = "DELETE FROM tabel_panen WHERE id=" . $id;
$hapus = mysqli_query($koneksi, $sql_hapus);
if($hapus){
if($_GET['aksi'] == 'delete'){
header('location: index.php');
}
}
}
}
// --- Tutup Fungsi Hapus
// ===================================================================
// --- Program Utama
if (isset($_GET['aksi'])){
switch($_GET['aksi']){
case "create":
echo '<a href="index.php"> « Home</a>';
tambah($koneksi);
break;
case "read":
tampil_data($koneksi);
break;
case "update":
ubah($koneksi);
tampil_data($koneksi);
break;
case "delete":
hapus($koneksi);
break;
default:
echo "<h3>Aksi <i>".$_GET['aksi']."</i> tidaka ada!</h3>";
tambah($koneksi);
tampil_data($koneksi);
}
} else {
tambah($koneksi);
tampil_data($koneksi);
}
?>
</body>
</html>