Praktik 4
koneksi.php
<?php
$con = mysqli_connect("localhost", "root", "", "pwdpb");
if (!$con) {
echo "Koneksi Gagal";
}
function hapus ($data) {
global $con;
$id = $_GET['id'];
$sql = mysqli_query($con, "DELETE FROM praktek1 WHERE id = '$id'");
return mysqli_affected_rows($con);
}
function edit($data) {
global $con;
$id = $_GET['id'];
$nama = $_POST['nama'];
$sql = "UPDATE praktek1 SET
nama = '$nama'
WHERE id = '$id'";
$query = mysqli_query($con, $sql);
return mysqli_affected_rows($con);
}
?>
edit.php
<?php
require 'koneksi.php';
$id = $_GET['id'];
if (isset($_POST['submit'])) {
if (edit($_POST) > 0) {
echo "
<script>
alert('Data Berhasil Diubah');
document.location.href = 'index.php';
</script>
";
} else {
echo "
<script>
alert('Buku Gagal Diubah');
// document.location.href = 'buku_admin.php';
</script>
";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.6/dist/umd/popper.min.js" integrity="sha384-oBqDVmMz9ATKxIep9tiCxS/Z9fNfEXiDAYTujMAeBAsjFuCZSmKbSSUnQlmh/jp3" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.min.js" integrity="sha384-IDwe1+LCz02ROU9k972gdyvl+AESN10+x7tBKgc9I5HFtuNz0wWnPclzo6p9vxnk" crossorigin="anonymous"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.7/css/select2.min.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-3.4.1.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.7/js/select2.min.js"></script>
<style type="text/css">
body {
font-family: Bahnschrift;
background: url(' bckgrnd.JPG');
background-size: cover;
background-repeat: no-repeat;
background-position: center;
background-attachment: fixed;
}
.card{
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
border: 5px solid #a7aae8;
padding: 10px;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md">
<form method="POST">
<?php
$sql = mysqli_query($con, "SELECT * FROM praktek1 WHERE id = '$id'");
foreach ($sql as $row) {
?>
<div class="card" style="width: 25rem;">
<div class="card-body">
<h5 class="card-title text-center">Edit Data</h5>
<input type="number" name="id" value="<?php echo $id; ?>" hidden>
<div class="form-floating mb-3">
<input type="text" class="form-control" name="nama" id="nama" value="<?php echo $row["nama"]; ?>">
<label for="nama">Nama</label>
</div>
</div>
<?php } ?>
<input type="submit" name="submit" value="Edit Data" class="btn btn-warning mb-3 fw-bold">
</div>
</form>
</div>
</div>
<!-- <div class="row">
<div class="col-md">
<div class="form-floating mb-3">
<input type="number" class="form-control" name="isbn" id="isbn" placeholder="isbn">
<label for="isbn">ISBN</label>
</div>
</div>
</div> -->
</div>
<script type="text/javascript">
$(document).ready(function() {
$('#penerbit').select2();
$('#genre').select2();
});
</script>
</body>
</html>
index.php
<?php
require 'koneksi.php';
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.6/dist/umd/popper.min.js" integrity="sha384-oBqDVmMz9ATKxIep9tiCxS/Z9fNfEXiDAYTujMAeBAsjFuCZSmKbSSUnQlmh/jp3" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.min.js" integrity="sha384-IDwe1+LCz02ROU9k972gdyvl+AESN10+x7tBKgc9I5HFtuNz0wWnPclzo6p9vxnk" crossorigin="anonymous"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.7/css/select2.min.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-3.4.1.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.7/js/select2.min.js"></script>
<style type="text/css">
body {
font-family: Bahnschrift;
background: url(' bckgrnd.JPG');
background-size: cover;
background-repeat: no-repeat;
background-position: center;
background-attachment: fixed;
}
table {
background-color: #a7aae8;
}
</style>
</head>
<body>
<div class="container">
<div class="row justify-content-center">
<div class="col-md">
<div class="table-responsive">
<table class="table table-bordered table-hover mt-5">
<thead>
<th>No</th>
<th>Nama</th>
<th>Aksi</th>
</thead>
<?php
$no = 1;
$sql = mysqli_query($con, "SELECT * FROM praktek1");
foreach ($sql as $data) {
?>
<tbody>
<tr>
<td><?php echo $no++; ?></td>
<td><?php echo $data["nama"]; ?></td>
<td>
<a href="hapus.php?id=<?= $data["id"]; ?>" class="btn btn-danger fw-bold mb-3" style="text-decoration-line: none;">
<i>
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor" class="bi bi-trash-fill" viewBox="0 0 16 16">
<path d="M2.5 1a1 1 0 0 0-1 1v1a1 1 0 0 0 1 1H3v9a2 2 0 0 0 2 2h6a2 2 0 0 0 2-2V4h.5a1 1 0 0 0 1-1V2a1 1 0 0 0-1-1H10a1 1 0 0 0-1-1H7a1 1 0 0 0-1 1H2.5zm3 4a.5.5 0 0 1 .5.5v7a.5.5 0 0 1-1 0v-7a.5.5 0 0 1 .5-.5zM8 5a.5.5 0 0 1 .5.5v7a.5.5 0 0 1-1 0v-7A.5.5 0 0 1 8 5zm3 .5v7a.5.5 0 0 1-1 0v-7a.5.5 0 0 1 1 0z"/>
</svg>
</i>
</a>
<a href="edit.php?id=<?= $data["id"]; ?>" class="btn btn-warning fw-bold mb-3" style="text-decoration-line: none;">
<i>
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor" class="bi bi-pencil-square" viewBox="0 0 16 16">
<path d="M15.502 1.94a.5.5 0 0 1 0 .706L14.459 3.69l-2-2L13.502.646a.5.5 0 0 1 .707 0l1.293 1.293zm-1.75 2.456-2-2L4.939 9.21a.5.5 0 0 0-.121.196l-.805 2.414a.25.25 0 0 0 .316.316l2.414-.805a.5.5 0 0 0 .196-.12l6.813-6.814z"/>
<path fill-rule="evenodd" d="M1 13.5A1.5 1.5 0 0 0 2.5 15h11a1.5 1.5 0 0 0 1.5-1.5v-6a.5.5 0 0 0-1 0v6a.5.5 0 0 1-.5.5h-11a.5.5 0 0 1-.5-.5v-11a.5.5 0 0 1 .5-.5H9a.5.5 0 0 0 0-1H2.5A1.5 1.5 0 0 0 1 2.5v11z"/>
</svg>
</i>
</a>
</td>
</tr>
</tbody>
<?php } ?>
</table>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function() {
$('#penerbit').select2();
$('#genre').select2();
});
</script>
</body>
</html>
hapus.php
<?php
require '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