-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
73 lines (57 loc) · 1.87 KB
/
functions.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
<?php
$hostname = 'localhost';
$username = 'root';
$password = '';
$database = 'uniqlone';
$mysqli = new mysqli($hostname, $username, $password, $database);
if ($mysqli->connect_error) {
die("Koneksi ke database gagal: " . $mysqli->connect_error);
}
function createProduk($nama, $kategori, $deskripsi, $gambar)
{
global $mysqli;
$query = "INSERT INTO produk (nama, kategori, deskripsi, gambar) VALUES (?, ?, ?, ?)";
$stmt = $mysqli->prepare($query);
$stmt->bind_param("ssss", $nama, $kategori, $deskripsi, $gambar);
$target_dir = "uploads/";
$target_file = $target_dir . basename($gambar);
move_uploaded_file($_FILES["gambar"]["tmp_name"], $target_file);
$stmt->execute();
$stmt->close();
}
function readProduk()
{
global $mysqli;
$query = "SELECT * FROM produk";
$product = $mysqli->query($query);
return $product;
}
function updateProduk($id, $nama, $kategori, $deskripsi, $gambar)
{
global $mysqli;
if (!empty($gambar)) {
$query = "UPDATE produk SET nama=?, kategori=?, deskripsi=?, gambar=? WHERE id=?";
$stmt = $mysqli->prepare($query);
$stmt->bind_param("ssssi", $nama, $kategori, $deskripsi, $gambar, $id);
$target_dir = "uploads/";
$target_file = $target_dir . basename($gambar);
move_uploaded_file($_FILES["edit_gambar"]["tmp_name"], $target_file);
$stmt->execute();
$stmt->close();
} else {
$query = "UPDATE produk SET nama=?, kategori=?, deskripsi=? WHERE id=?";
$stmt = $mysqli->prepare($query);
$stmt->bind_param("sssi", $nama, $kategori, $deskripsi, $id);
$stmt->execute();
$stmt->close();
}
}
function deleteProduk($id)
{
global $mysqli;
$query = "DELETE FROM produk WHERE id = ?";
$stmt = $mysqli->prepare($query);
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->close();
}