-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsiswa.php
More file actions
135 lines (124 loc) · 3.72 KB
/
siswa.php
File metadata and controls
135 lines (124 loc) · 3.72 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
<?php
include 'koneksi.php';
$nis = '';
$nama = '';
$kelas = '';
$alamat = '';
$tgl_masuk = '';
$is_edit = false;
// CREATE & UPDATE
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$nis = $_POST['nis'];
$nama = $_POST['nama'];
$kelas = $_POST['kelas'];
$alamat = $_POST['alamat'];
$tgl_masuk = $_POST['tgl_masuk'];
if (isset($_POST['update'])) {
// UPDATE
$sql = "UPDATE Siswa SET nama='$nama', kelas='$kelas', alamat='$alamat', tgl_masuk='$tgl_masuk' WHERE nis='$nis'";
} else {
// CREATE
$sql = "INSERT INTO Siswa (nis, nama, kelas, alamat, tgl_masuk) VALUES ('$nis', '$nama', '$kelas', '$alamat', '$tgl_masuk')";
}
$koneksi->query($sql);
header("Location: " . $_SERVER['PHP_SELF']);
exit();
}
// DELETE
if (isset($_GET['aksi']) && $_GET['aksi'] == 'hapus') {
$nis_hapus = $_GET['nis'];
$koneksi->query("DELETE FROM Siswa WHERE nis='$nis_hapus'");
header("Location: " . $_SERVER['PHP_SELF']);
exit();
}
// READ (untuk form edit)
if (isset($_GET['aksi']) && $_GET['aksi'] == 'edit') {
$is_edit = true;
$nis_edit = $_GET['nis'];
$result = $koneksi->query("SELECT * FROM Siswa WHERE nis='$nis_edit'");
$data = $result->fetch_assoc();
if ($data) {
$nis = $data['nis'];
$nama = $data['nama'];
$kelas = $data['kelas'];
$alamat = $data['alamat'];
$tgl_masuk = $data['tgl_masuk'];
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Data Siswa</title>
</head>
<body>
<h3><?php echo $is_edit ? 'Edit' : 'Tambah'; ?> Data Siswa</h3>
<form action="" method="POST">
<table cellpadding="4">
<tr>
<td>NIS</td>
<td><input type="text" name="nis" value="<?php echo $nis; ?>" <?php echo $is_edit ? 'readonly' : 'required'; ?>></td>
</tr>
<tr>
<td>Nama</td>
<td><input type="text" name="nama" value="<?php echo $nama; ?>" required></td>
</tr>
<tr>
<td>Kelas</td>
<td><input type="text" name="kelas" value="<?php echo $kelas; ?>"></td>
</tr>
<tr>
<td>Alamat</td>
<td><input type="text" name="alamat" value="<?php echo $alamat; ?>"></td>
</tr>
<tr>
<td>Tgl Masuk</td>
<td><input type="date" name="tgl_masuk" value="<?php echo $tgl_masuk; ?>" required></td>
</tr>
<tr>
<td></td>
<td>
<?php if ($is_edit): ?>
<button type="submit" name="update">Update</button>
<?php else: ?>
<button type="submit" name="simpan">Simpan</button>
<?php endif; ?>
</td>
</tr>
</table>
</form>
<hr>
<h3>Daftar Siswa</h3>
<table border="1" cellpadding="5" cellspacing="0">
<thead>
<tr>
<th>NIS</th>
<th>Nama</th>
<th>Kelas</th>
<th>Alamat</th>
<th>Tgl Masuk</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
<?php
$result = $koneksi->query("SELECT * FROM Siswa ORDER BY nama ASC");
while ($row = $result->fetch_assoc()):
?>
<tr>
<td><?php echo $row['nis']; ?></td>
<td><?php echo $row['nama']; ?></td>
<td><?php echo $row['kelas']; ?></td>
<td><?php echo $row['alamat']; ?></td>
<td><?php echo $row['tgl_masuk']; ?></td>
<td>
<a href="?aksi=edit&nis=<?php echo $row['nis']; ?>">Edit</a> |
<a href="?aksi=hapus&nis=<?php echo $row['nis']; ?>" onclick="return confirm('Yakin?');">Hapus</a>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
<?php $koneksi->close(); ?>
</body>
</html>