-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
131 lines (109 loc) · 3.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
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
<?php
// koneksi ke database
$host = "localhost";
$username = "root";
$password = "";
$database = "pweb";
$conn = mysqli_connect($host, $username, $password, $database);
function debug($data){
var_dump($data);
die;
}
// Bikin fungsi query sendiri
function query($query){
global $conn;
$result = mysqli_query($conn, $query);
// buat nyimpen per-row
$rows = [];
while ($row = mysqli_fetch_assoc($result)){
$rows[] = $row;
}
return $rows;
}
function upload(){
$nama = $_FILES["image"]["name"];
$ukuran = $_FILES["image"]["size"];
$error = $_FILES["image"]["error"];
$tmpName = $_FILES["image"]["tmp_name"];
// debug($tmpName);
// cek apakah ada gambar yang diupload
if($error === 4){
echo "<script>
alert('Upload gambar telerbih dahulu')
</script>";
return false;
}
// cek apakah yang diupload adalah gambar
$extValid = ["jpg", "jpeg", "png"];
$extFiles = explode(".", $nama);
$extFiles = strtolower(end($extFiles));
// debug($extFiles);
if(!in_array($extFiles, $extValid)){
echo "<script>
alert('Tolong hanya upload gambar')
</script>";
return false;
}
// cek jika ukurannya terlalu besar
if($ukuran > 1000000){
echo "<script>
alert('Ukuran file terlalu besar')
</script>";
return false;
}
$newNama = uniqid();
$newNama .= "." . $extFiles;
move_uploaded_file($tmpName, "./img/" . $newNama);
return $newNama;
}
function insert($data){
global $conn;
// ambil data dari tiap input
$name = htmlspecialchars($data["name"]);
$address = htmlspecialchars($data["address"]);
$gender = htmlspecialchars($data["gender"]);
$religion = htmlspecialchars($data["religion"]);
$school_origin = htmlspecialchars($data["school_origin"]);
// upload files
$img = upload();
if(!$img){
return false;
}
//query insert data
$query = "INSERT INTO students (name, address, gender, religion, school_origin, image) VALUES
('$name', '$address', '$gender', '$religion', '$school_origin', '$img')";
mysqli_query($conn, $query);
// debug($school_origin);
return mysqli_affected_rows($conn);
}
function update($data, $id){
global $conn;
// ambil data dari tiap input
$name = htmlspecialchars($data["name"]);
$address = htmlspecialchars($data["address"]);
$gender = htmlspecialchars($data["gender"]);
$religion = htmlspecialchars($data["religion"]);
$school_origin = htmlspecialchars($data["school_origin"]);
$old_image = htmlspecialchars($data["image"]);
if($_FILES["image"]["error"] === 4){
$image = $old_image;
}else{
$image = upload();
}
//query update data
$query = "UPDATE students SET
name='$name',
address='$address',
gender='$gender',
religion='$religion',
school_origin='$school_origin',
image='$image'
WHERE id=$id";
mysqli_query($conn, $query);
return mysqli_affected_rows($conn);
}
function delete($id){
global $conn;
mysqli_query($conn, "DELETE FROM students WHERE id=$id");
return mysqli_affected_rows($conn);
}