-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
171 lines (135 loc) · 4.21 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
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
<?php
// terhubung database
// $conn = mysqli_connect('sql305.epizy.com', 'epiz_26613537', 'MJcWXmrdkF', 'epiz_26613537_mulaiphp');
$conn = mysqli_connect('localhost', 'root', '', 'mulaiphp');
// terhubung table dalam database dan mengambil setiap baris
function query($query)
{
global $conn;
$result = mysqli_query($conn, $query);
$kotak = [];
while ($row = mysqli_fetch_assoc($result)) {
$kotak[] = $row;
}
return $kotak;
}
// menambahkan data anggota keluarga
function tambah($data)
{
global $conn;
$nama = htmlspecialchars($data["nama"]);
$kode = htmlspecialchars($data["kode"]);
$pekerjaan = htmlspecialchars($data["pekerjaan"]);
// Upload gambar
$gambar = upload();
if (!$gambar) {
return false;
}
$query = "INSERT INTO keluarga VALUES
('', '$nama', '$kode', '$pekerjaan', '$gambar')";
mysqli_query($conn, $query);
return mysqli_affected_rows($conn);
}
// upload gambar
function upload()
{
$namaFile = $_FILES["gambar"]["name"];
$ukuranFile = $_FILES["gambar"]["size"];
$error = $_FILES["gambar"]["error"];
$tmpName = $_FILES["gambar"]["tmp_name"];
// cek upload gambar
if ($error === 4) {
echo "<script>
alert('Wajib pilih gambar!');
</script>";
return false;
}
//cek ekstensi
$ekstensiValid = ['jpeg', 'jpg', 'png'];
$ekstensiGambar = explode('.', $namaFile);
$ekstensiGambar = strtolower(end($ekstensiGambar));
if (!in_array($ekstensiGambar, $ekstensiValid)) {
echo "<script>
alert('Wajib pilih file gambar!');
</script>";
return false;
}
//menghasilkan nama acak
$namaFileBaru = uniqid();
$namaFileBaru .= '.';
$namaFileBaru .= $ekstensiGambar;
//cek ukuran
if ($ukuranFile > 1000000) {
echo "<script>
alert('Ukuran gambar terlalu besar!');
</script>";
return false;
}
move_uploaded_file($tmpName, 'img/' . $namaFileBaru);
return $namaFileBaru;
}
// Hapus anggota keluarga
function hapus($id)
{
global $conn;
mysqli_query($conn, "DELETE FROM keluarga WHERE id = $id");
return mysqli_affected_rows($conn);
}
// Mengubah data anggota keluarga
function ubah($data)
{
global $conn;
$id = $data["id"];
$nama = htmlspecialchars($data["nama"]);
$kode = htmlspecialchars($data["kode"]);
$pekerjaan = htmlspecialchars($data["pekerjaan"]);
$gambarLama = htmlspecialchars($data["gambarLama"]);
if ($_FILES["gambar"]["error"] === 4) {
$gambar = $gambarLama;
} else {
$gambar = upload();
}
$query = "UPDATE keluarga SET
nama = '$nama',
kode = '$kode',
pekerjaan = '$pekerjaan',
gambar = '$gambar'
WHERE id = $id";
mysqli_query($conn, $query);
return mysqli_affected_rows($conn);
}
// Cari data anggota keluarga
function cari($keyword)
{
$query = "SELECT * FROM keluarga WHERE
nama LIKE '%$keyword%' OR
kode LIKE '%$keyword%' OR
pekerjaan LIKE '%$keyword%'
";
return query($query);
}
// Registrasi admin
function regis($data) {
global $conn;
$user = strtolower(stripslashes($data["username"]));
$pass = mysqli_real_escape_string($conn, $data["password"]);
$pass2 = mysqli_real_escape_string($conn, $data["password2"]);
$result= mysqli_query($conn, "SELECT username FROM pengguna WHERE username = '$user'");
if ( mysqli_fetch_assoc($result) ) {
echo "<script>
alert('Username sudah dipakai');
</script>";
return false;
}
//cek kedua password
if ( $pass !== $pass2 ) {
echo "<script>
alert('Password tidak cocok');
</script>";
return false;
}
//enkripsi
$pass = password_hash($pass, PASSWORD_DEFAULT);
mysqli_query($conn, "INSERT INTO pengguna VALUES ('','$user', '$pass')");
return mysqli_affected_rows($conn);
}