-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
182 lines (156 loc) · 5.02 KB
/
Copy pathfunctions.php
File metadata and controls
182 lines (156 loc) · 5.02 KB
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
<?php
// Database Connection
$conn = mysqli_connect("localhost", "root", "root", "phpdasar");
// Check if the connection was successful
if (!$conn) {
// Output error message and stop script execution if the connection fails
die("Connection failed: " . mysqli_connect_error());
}
// Function Definition
function query($query){
global $conn;
// Query Execution
$result = mysqli_query($conn, $query);
// Fetching Results
$rows = [];
while ($row = mysqli_fetch_assoc($result)){
$rows[] = $row;
}
return $rows;
}
// function add data
function add($data) {
global $conn; // htmlspecialchars ni important in term of security cause nk elak dari kene kacau masa user insert form "mcm distrup dari code html di form
$nrp = htmlspecialchars($data["nrp"]);
$name = htmlspecialchars($data["name"]);
$email = htmlspecialchars($data["email"]);
$jurusan = htmlspecialchars($data["jurusan"]);
//Upload photo dlu
$gambar = upload();
if( !$gambar){
return false;
}
// Query insert data
$query = "INSERT INTO mahasiswa (nrp, name, email, jurusan, gambar)
VALUES ('$nrp', '$name', '$email', '$jurusan', '$gambar')";
// Execute the query
mysqli_query($conn, $query);
return mysqli_affected_rows($conn);
}
// Function upload file
function upload(){
$namaFile = $_FILES['gambar']['name'];
$ukuranFile = $_FILES['gambar']['size'];
$error = $_FILES['gambar']['error'];
$tmpName = $_FILES['gambar']['tmp_name'];
// Check if no image is uploaded and display an alert message
if($error === 4){
echo"
<script>
alert('Choose a file first!');
</script>";
return false;
}
// Check if the uploaded file is a photo or not and display an alert message if not
$ekstensiGambarValid = ['jpg', 'jpeg', 'png'];
$ekstensiGambar = explode('.', $namaFile);
$ekstensiGambar = strtolower(end($ekstensiGambar));
if( !in_array($ekstensiGambar, $ekstensiGambarValid)) {
echo"
<script>
alert('You did not upload a valid photo!');
</script>";
return false;
}
// Check if the size of the file is too big and display an alert message if it is
if($ukuranFile > 10000000){
echo"
<script>
alert('The photo size is too big!');
</script>";
return false;
}
// Generate a unique name for the file
$namaFileBaru = uniqid() . '.' . $ekstensiGambar;
// Move the uploaded file to the img/ directory with the new name
move_uploaded_file($tmpName, 'img/' . $namaFileBaru);
return $namaFileBaru;
}
// function delete data
function delete($id) {
global $conn;
mysqli_query($conn, "DELETE FROM mahasiswa WHERE id = $id");
return mysqli_affected_rows($conn);
}
// function edit data
function edit($data){
global $conn;
$id = $data["id"];
$nrp = htmlspecialchars($data["nrp"]);
$name = htmlspecialchars($data["name"]);
$email = htmlspecialchars($data["email"]);
$jurusan = htmlspecialchars($data["jurusan"]);
$gambarlama = htmlspecialchars($data["gambarlama"]);
// check if user choose new photo or not
if( $_FILES['gambar']['error'] === 4){
$gambar = $gambarlama;
} else{
$gambar = upload();
}
// Query update data
$query = "UPDATE mahasiswa SET
nrp = '$nrp',
name = '$name',
email = '$email',
jurusan = '$jurusan',
gambar = '$gambar'
WHERE id = $id";
// Execute the query
mysqli_query($conn, $query);
return mysqli_affected_rows($conn);
}
// function search data
function search($keyword){
$query = "SELECT * FROM mahasiswa
WHERE
name LIKE '%$keyword%' OR
nrp LIKE '%$keyword%' OR
name LIKE '%$keyword%' OR
email LIKE '%$keyword%' OR
jurusan LIKE '%$keyword%'
"; // ni element penting kalau nk buat search
return query($query);
}
// Function registration
function registration($data) {
global $conn;
$username = strtolower(stripslashes($data["username"]));
$password = mysqli_real_escape_string($conn, $data["password"]);
$password2 = mysqli_real_escape_string($conn, $data["password2"]);
$result = mysqli_query($conn, "SELECT username FROM user WHERE
username = '$username'");
if(mysqli_fetch_assoc($result)){
echo "
<script>
alert('Sorry username already registered. Create other username');
</script>
";
return false; // supaya x masuk dlm database
}
// Check confirmation pass
if($password !== $password2) {
echo"
<script>
alert('Confirmation password not valid. Try again!');
</script>
";
return false;
}
//Encryption dulu password
$password = password_hash($password, PASSWORD_DEFAULT);
//Insert ke database
mysqli_query($conn, "INSERT INTO user (username, password)
VALUES('$username', '$password')");
return mysqli_affected_rows($conn);
}
?>