-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.php
141 lines (114 loc) · 3.12 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
<?php
$server = "localhost";
$user = "root";
$password = "";
$db = "be_practice";
// Create connection
$conn = mysqli_connect($server, $user, $password, $db);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
function query($query)
{
global $conn;
$result = mysqli_query($conn, $query);
$rows = [];
while ($row = mysqli_fetch_assoc($result)) {
$rows[] = $row;
}
return $rows;
}
function tambah($data)
{
global $conn;
$nama = htmlspecialchars($data["Nama"]);
$alamat = htmlspecialchars($data["Alamat"]);
$hobi = htmlspecialchars($data["Hobi"]);
// Upload Picture
$foto = upload();
if (!$foto) {
return false;
}
$query = "INSERT INTO test VALUES
('','$nama','$alamat','$hobi','$foto')";
mysqli_query($conn, $query);
return mysqli_affected_rows($conn);
}
function upload()
{
$namaFoto = $_FILES['Foto']['name'];
$ukuranFoto = $_FILES['Foto']['size'];
$error = $_FILES['Foto']['error'];
$tmpName = $_FILES['Foto']['tmp_name'];
// Picture Uploaded Check
if ($error === 4) {
echo " <script>
alert('Pilih gambar terlebih dahulu!')
</script>";
return false;
}
// Extension Picture Allowed Check
$ekstensiFotoValid = ['jpg', 'jpeg', 'png'];
$ekstensiFoto = explode('.', $namaFoto);
$ekstensiFoto = strtolower(end($ekstensiFoto));
if (!in_array($ekstensiFoto, $ekstensiFotoValid)) {
echo " <script>
alert('Hanya dapat mengupload file gambar!')
</script>";
return false;
}
// Picture's Size Limit Check
// Generate Picture's Name
$namaFotoBaru = uniqid();
$namaFotoBaru .= '.';
$namaFotoBaru .= $ekstensiFoto;
if ($ukuranFoto > 5000000) {
echo " <script>
alert('Maksimal ukuran file yaitu 5 MB!')
</script>";
return false;
}
// Picture ready to upload
move_uploaded_file($tmpName, 'img/' . $namaFotoBaru);
return $namaFotoBaru;
}
function hapus($id)
{
global $conn;
mysqli_query($conn, "DELETE FROM test WHERE ID = $id");
return mysqli_affected_rows($conn);
}
function ubah($data)
{
global $conn;
$id = $data["ID"];
$nama = htmlspecialchars($data["Nama"]);
$alamat = htmlspecialchars($data["Alamat"]);
$hobi = htmlspecialchars($data["Hobi"]);
$fotoLama = htmlspecialchars($data["fotoLama"]);
// Choose New Photo Check
if ($_FILES['Foto']['error'] === 4) {
$foto = $fotoLama;
} else {
$foto = upload();
}
$query = "UPDATE test SET
Nama = '$nama',
Alamat = '$alamat',
Hobi = '$hobi',
Foto = '$foto'
WHERE id = $id
";
mysqli_query($conn, $query);
return mysqli_affected_rows($conn);
}
function cari($keyword)
{
$query = "SELECT * FROM test
WHERE Nama LIKE '%$keyword%'
OR Alamat LIKE '%$keyword%'
OR Hobi LIKE '%$keyword%'
";
return query($query);
}