Part 2
Koneksi.php
<?php
$koneksi = mysqli_connect("localhost","root", "", "siswa");
function hapus($data){
global $koneksi;
$id = $_GET['id'];
$sql = mysqli_query($koneksi, "DELETE FROM sajk WHERE id = '$id'");
return mysqli_affected_rows($koneksi);
}
function edit($data){
global $koneksi;
$id = $_GET['id'];
$nama = $_POST['nama'];
$jenkel = $_POST['jenkel'];
$sql = "UPDATE sajk SET nama = '$nama', jenkel = '$jenkel' WHERE id ='$id'";
$query = mysqli_query($koneksi,$sql);
return mysqli_affected_rows($koneksi);
}
?>
Index.php
<!DOCTYPE html>
<html>
<head>
<title>Update Delete Projek 3</title>
</head>
<body>
<center>
<table border="2" cellpadding="10">
<tr>
<th>ID</th>
<th>Nama</th>
<th>Jenis Kelamin</th>
<th>Aksi</th>
</tr>
<?php
include 'koneksi.php';
$sql = mysqli_query($koneksi, "SELECT * FROM sajk");
foreach ($sql as $data) {
?>
<tr>
<td><?php echo $data["id"];?></td>
<td><?php echo $data["nama"];?></td>
<td><?php echo $data["jenkel"];?></td>
<td><a href="hapus.php?id=<?=$data["id"];?>">hapus</a>
<a href="edit.php?id=<?=$data["id"];?>">edit</a></td>
</tr>
<?php } ?>
</table></center>
</body>
</html>
Edit.php
<?php
include 'koneksi.php';
$id = $_GET['id'];
if (isset($_POST['submit'])) {
if (edit($_POST)>0) {
echo "<script>alert('data berhasil diedit');
document.location.href = 'index.php';
</script>";
}else{
echo "<script>alert('data gagal diedit');
</script>";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>EDIT</title>
</head>
</style>
<body>
<center>
<form method="POST">
<?php
$sql = mysqli_query($koneksi,"SELECT * FROM sajk WHERE id = '$id'");
foreach ($sql as $row) {
?>
<table cellpadding="13">
<tr>
<th colspan="2"><h2>EDIT DATA</h2></th>
</tr>
<tr>
<td colspan="2"><input type="number" name="id" value="<?php echo $id; ?>" hidden></td>
</tr>
<tr>
<td><label>Nama</label></td>
<td><input type="text" name="nama" id="nama" value="<?php echo $row["nama"];?>"></td>
</tr>
<tr>
<td><label>Jenis Kelamin</label></td>
<td><?php
$jenkel = $row['jenkel'];
if ($jenkel == "Laki-laki") {
echo '<input type="radio" name="jenkel" value="Laki-laki" checked>Laki-laki
<input type="radio" name="jenkel" value="Perempuan">Perempuan';
}elseif ($jenkel == "Perempuan") {
echo '<input type="radio" name="jenkel" value="Perempuan" checked>Perempuan
<input type="radio" name="jenkel" value="Laki-laki">Laki-laki';
}
?></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit"></td>
</tr>
</table>
<?php } ?>
</form>
</center>
</body>
</html>
Hapus.php
<?php
include 'koneksi.php';
$id = $_GET['id'];
if (hapus($id)>0) {
echo "
<script>alert('data berhasil dihapus');
document.location.href = 'index.php';
</script>";
}else{
echo "
<script>alert('data gagal dihapus');
</script>";
}
?>
Komentar
Posting Komentar