-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrud-php.php
204 lines (197 loc) · 7.75 KB
/
crud-php.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "magang";
$koneksi = mysqli_connect($host,$user,$pass,$db);
if(!$koneksi){ //cek koneksi
die("Tidak bisa terkoneksi ke database");
}
$nim = "";
$nama = "";
$alamat = "";
$fakultas = "";
$sukses = "";
$error = "";
if (isset($_GET['op'])) {
$op = $_GET['op'];
} else {
$op = "";
}
if($op == 'delete'){
$id = $_GET['id'];
$sql1 = "delete from mahasiswa where id = '$id'";
$q1 = mysqli_query($koneksi,$sql1);
if($q1){
$sukses = "Berhasil hapus data";
}else{
$error = "Gagal melakukan delete data";
}
}
if ($op == 'edit') {
$id = $_GET['id'];
$sql1 = "select * from mahasiswa where id = '$id'";
$q1 = mysqli_query($koneksi, $sql1);
$r1 = mysqli_fetch_array($q1);
$nim = $r1['nim'];
$nama = $r1['nama'];
$alamat = $r1['alamat'];
$fakultas = $r1['fakultas'];
if ($nim == '') {
$error = "Data tidak ditemukan";
}
}
if (isset($_POST['simpan'])) { //untuk membuat
$nim = $_POST['nim'];
$nama = $_POST['nama'];
$alamat = $_POST['alamat'];
$fakultas = $_POST['fakultas'];
if ($nim && $nama && $alamat && $fakultas) {
if ($op == 'edit') { //untuk update
$sql1 = "update mahasiswa set nim = '$nim',nama='$nama',alamat = '$alamat',fakultas='$fakultas' where id = '$id'";
$q1 = mysqli_query($koneksi, $sql1);
if ($q1) {
$sukses = "Data berhasil diupdate";
} else {
$error = "Data gagal diupdate";
}
} else { //untuk insert
$sql1 = "insert into mahasiswa(nim,nama,alamat,fakultas) values ('$nim','$nama','$alamat','$fakultas')";
$q1 = mysqli_query($koneksi, $sql1);
if ($q1) {
$sukses = "Berhasil memasukkan data baru";
} else {
$error = "Gagal memasukkan data";
}
}
} else {
$error = "Silakan masukkan semua data";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Magang mahasiswa</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css" integrity="sha384-zCbKRCUGaJDkqS1kPbPd7TveP5iyJE0EjAuZQTgFLD2ylzuqKfdKlfG/eSrtxUkn" crossorigin="anonymous">
<style>
.mx-auto { width:800px }
.card { margin-top: 10px;}
</style>
</head>
<body>
<div class="mx-auto">
<!--UNTUK MEMASUKAN DATA-->
<div class="card">
<div class="card-header">
Create /Edit Data
</div>
<div class="card-body">
<?php
if($error){
?>
<div class="alert alert-danger" role="alert">
<?php echo $error ?>
</div>
<?php
header("refresh:1;url=index.php");
}
?>
<?php
if($sukses){
?>
<div class="alert alert-success d-flex align-items-center" role="alert">
<?php echo $sukses ?>
</div>
<?php
header("refresh:1;url=index.php");
}
?>
<form action="" method="POST">
<div class="mb-3 row">
<label for="nim" class="col-sm-2 col-form-label">NIM</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="nim" name="nim" value="<?php echo $nim ?>">
</div>
</div>
<div class="mb-3 row">
<label for="nama" class="col-sm-2 col-form-label">Nama</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="nama" name="nama" value="<?php echo $nama ?>">
</div>
</div>
<div class="mb-3 row">
<label for="alamat" class="col-sm-2 col-form-label">Alamat</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="alamat" name="alamat" value="<?php echo $alamat ?>">
</div>
</div>
<div class="mb-3 row">
<label for="fakultas" class="col-sm-2 col-form-label">Fakultas</label>
<div class="col-sm-10">
<select class="form-control" name="fakultas" id="fakultas">
<option value="">- Pilih Fakultas -</option>
<option value="saintek" <?php if($fakultas == "saintek") echo "selected" ?>>saintek</option>
<option value="saintek" <?php if($fakultas == "soshum") echo "selected" ?>>soshum</option>
</select>
</div>
</div>
<div class="col-12">
<input type="submit" name="simpan" value="Simpan Data" class="btn btn-primary">
</div>
</form>
</div>
</div>
<!---UNTUK MENGELUARKAN DATA-->
<div class="card">
<div class="card-header text-white bg-secondary">
Data Magang mahasiswa
</div>
<div class="card-body">
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">NIM</th>
<th scope="col">Nama</th>
<th scope="col">Alamat</th>
<th scope="col">Fakultas</th>
<th scope="col">Aksi</th>
</tr>
</thead>
<tbody>
<?php
$sql2 = "select * from mahasiswa order by id desc";
$q2 = mysqli_query($koneksi, $sql2);
$urut = 1;
while ($r2 = mysqli_fetch_array($q2)) {
$id = $r2['id'];
$nim = $r2['nim'];
$nama = $r2['nama'];
$alamat = $r2['alamat'];
$fakultas = $r2['fakultas'];
?>
<tr>
<th scope="row"><?php echo $urut++ ?></th>
<td scope="row"><?php echo $nim ?></td>
<td scope="row"><?php echo $nama ?></td>
<td scope="row"><?php echo $alamat ?></td>
<td scope="row"><?php echo $fakultas ?></td>
<td scope="row">
<a href="index.php?op=edit&id=<?php echo $id ?>"><button type="button" class="btn btn-warning">Edit</button></a>
<a href="index.php?op=delete&id=<?php echo $id?>" onclick="return confirm('Yakin mau delete data?')"><button type="button" class="btn btn-danger">Delete</button></a>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>