-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
134 lines (109 loc) · 2.63 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
<?php
/**
* PHP CRUD DASAR
* Author : Irawan Kilmer
* Email : irawankillmer@gmail.com
* WhatsApp : 082318009200
*/
// ubah nama database, sesuaikan dengan nama database yang sudah dibuat
$conn = mysqli_connect('localhost', 'root', '', 'crud');
/**
* selectData
* Ambil semua data siswa
* @return void
*/
function selectData()
{
global $conn;
$query = mysqli_query($conn, "SELECT * FROM siswa");
$data = [];
while ($row = mysqli_fetch_assoc($query)) {
$data[] = $row;
}
return $data;
}
/**
* check
* Periksa Nisn atau Nis
* @param mixed $type
* @param mixed $data
* @return void
*/
function check(String $type, int $data)
{
global $conn;
$query = mysqli_query($conn, "SELECT * FROM siswa WHERE $type = '$data'");
$data = mysqli_num_rows($query);
return $data;
}
/**
* insertData
* Masukan data dari formulir ke dalam tabel siswa
* @param mixed $data
* @return void
*/
function insertData(Array $data)
{
global $conn;
$nisn = htmlspecialchars($data['nisn']);
$nis = htmlspecialchars($data['nis']);
$nama = htmlspecialchars($data['nama']);
mysqli_query($conn, "INSERT INTO siswa(`nisn`, `nis`, `nama`) VALUES('$nisn', '$nis', '$nama')");
if (mysqli_affected_rows($conn) > 0) {
echo "
<script>
alert('data berhasil ditambahkan'),
window.location.href='index.php'
</script>
";
}
}
/**
* deleteData
* Delete data siswa berdasarkan idSiswa
* @param mixed $id
* @return void
*/
function deleteData(int $id)
{
global $conn;
mysqli_query($conn, "DELETE FROM siswa WHERE idSiswa = '$id'");
if (mysqli_affected_rows($conn) > 0) {
echo "
<script>
alert('Data berhasil dihapus'),
window.location.href='index.php'
</script>
";
}
}
/**
* selectWhere
* Ambil data berdasarkan idSiswa
* @param mixed $id
* @return void
*/
function selectWhere(int $id)
{
global $conn;
$query = mysqli_query($conn, "SELECT * FROM siswa WHERE idSiswa='$id'");
$data = mysqli_fetch_assoc($query);
return $data;
}
function updateData(Array $data)
{
global $conn;
$id = $data['id'];
$nisn = $data['nisn'];
$nis = $data['nis'];
$nama = $data['nama'];
mysqli_query($conn, "UPDATE siswa SET nisn = '$nisn', nis = '$nis', nama = '$nama' WHERE idSiswa = '$id' ");
if (mysqli_affected_rows($conn) > 0) {
echo "
<script>
alert('Data berhasil di update')
window.location.href='index.php'
</script>
";
}
}