-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
127 lines (98 loc) · 2.89 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
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
<?php
$conn = mysqli_connect('localhost', 'root', '', 'db_tokoemha');
function query($query)
{
global $conn;
$result = mysqli_query($conn, $query);
$rows = [];
while ($row = mysqli_fetch_assoc($result)) {
$rows[] = $row;
}
return $rows;
}
function tambah($post)
{
global $conn;
$tgl_upload = time();
$gambar = upload();
if (!$gambar) {
return false;
}
mysqli_query($conn, "INSERT INTO galeri VALUES('', '$gambar', '$tgl_upload')");
return mysqli_affected_rows($conn);
}
function upload()
{
$nameFile = $_FILES['gambar']['name'];
$tmp_name = $_FILES['gambar']['tmp_name'];
$error = $_FILES['gambar']['error'];
if ($error === 4) {
echo "<script>
alert('tidak ada gambar yang diupload!');
</script>";
return false;
}
$ekstensiGambarValid = ['jpg', 'jpeg', 'png'];
$ekstensiGambar = explode('.', $nameFile);
$ekstensiGambar = strtolower(end($ekstensiGambar));
if (!in_array($ekstensiGambar, $ekstensiGambarValid)) {
echo "<script>
alert('pilih ekstension jpg, jpeg, png');
</script>";
return false;
}
$namaFileBaru = uniqid();
$namaFileBaru .= ".";
$namaFileBaru .= $ekstensiGambar;
move_uploaded_file($tmp_name, 'images/uploads/' . $namaFileBaru);
return $namaFileBaru;
}
function edit($post)
{
global $conn;
$id = $post["id"];
$gambarLama = $post["gambarLama"];
$tgl_upload = $post["tgl_upload"];
if ($_FILES["gambar"]["error"] === 4) {
$gambar = $gambarLama;
} else {
$gambar = upload();
}
$query = "UPDATE galeri SET
gambar = '$gambar',
tgl_upload = '$tgl_upload'
WHERE
id = $id
";
mysqli_query($conn, $query);
return mysqli_affected_rows($conn);
}
function hapus($id, $hlmn)
{
global $conn;
mysqli_query($conn, "DELETE FROM $hlmn WHERE id = $id");
return mysqli_affected_rows($conn);
}
// function daftar($post)
// {
// global $conn;
// $username = stripslashes(strtolower($post["username"]));
// $password = mysqli_real_escape_string($conn, $post["password"]);
// $password2 = mysqli_real_escape_string($conn, $post["password2"]);
// $result = mysqli_query($conn, "SELECT username FROM admins WHERE username = '$username'");
// if (mysqli_fetch_assoc($result)) {
// echo "<script>
// alert('username sudah terdaftar!');
// </script>";
// return false;
// }
// if ($password !== $password2) {
// echo "<script>
// alert('password tidak sesuai!');
// </script>";
// return false;
// }
// $password = password_hash($password, PASSWORD_DEFAULT);
// mysqli_query($conn, "INSERT INTO admins VALUES('', '$username', '$password')");
// return mysqli_affected_rows($conn);
// }