Index
<!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>Hobi</th>
<th>Aksi</th>
</tr>
<?php
include 'koneksi.php';
$sql = mysqli_query($koneksi, "SELECT * FROM tugas4");
foreach ($sql as $data) {
?>
<tr>
<td><?php echo $data["id"];?></td>
<td><?php echo $data["nama"];?></td>
<td><?php echo $data["hobi"];?></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>
Koneksi
<?php
$koneksi = mysqli_connect("localhost","root", "", "keempat");
function hapus($data){
global $koneksi;
$id = $_GET['id'];
$sql = mysqli_query($koneksi, "DELETE FROM tugas4 WHERE id = '$id'");
return mysqli_affected_rows($koneksi);
}
function edit($data){
global $koneksi;
$id = $_GET['id'];
$nama = $_POST['nama'];
$hobi = $row['hobi'];
$sql = "UPDATE tugas4 SET nama = '$_POST[nama]',
hobi = '$_POST[hobi]' WHERE id ='$id'";
$query = mysqli_query($koneksi,$sql);
return mysqli_affected_rows($koneksi);
}
?>
Edit
<?php
include "koneksi.php";
if(isset($_POST["submit"])) {
$id = $_POST['id'];
$hobi = implode(', ', $_POST['hobY']);
$sql = mysqli_query ($koneksi, "UPDATE tugas4 SET hobi = '$hobi' WHERE id = '$id' ");
header ('location:index.php?sukses');
}
$id = $_GET['id'];
$query_tampil = mysqli_query ($koneksi, "SELECT * FROM tugas4 WHERE id = '$id' ");
$edit = mysqli_fetch_array($query_tampil);
$checked = explode(', ', $edit['hobi']);
$query = mysqli_query($koneksi, "SELECT * FROM tugas4 ORDER BY id DESC");
?>
<style type="text/css">
table{
border-radius: 13%;
border: 2px solid;
border-color: black;
margin: 30px;
}
</style>
<form action="" method="post">
</form>
<html>
<head>
<title>P4</title>
</head>
<style type="text/css">
table{
border-radius: 13%;
border: 2px solid;
border-color: black;
margin: 30px;
}
</style>
<body>
<center>
<form method="POST">
<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>
<input type="text" name="nama" id="nama" value="<?php echo $edit["nama"];?>"></td>
</tr>
<tr>
<td><p>HOBI</p>
<input type="checkbox" name="hobY[]" value="Bola" <?php in_array ('Bola', $checked) ? print "checked" : ""; ?> > Bola<br>
<input type="checkbox" name="hobY[]" value="Basket" <?php in_array ('Basket', $checked) ? print "checked" : ""; ?> >Basket<br>
<input type="checkbox" name="hobY[]" value="Berenang" <?php in_array ('Berenang', $checked) ? print "checked" : ""; ?> >Berenang<br>
<input type="hidden" name="id" value="<?php echo $edit["id"]?>"/>
</td>
</tr>
</select>
<tr>
<td colspan="2"><input type="submit" name="submit"></td>
</tr>
</table>
</form>
</center>
</body>
</html>
Hapus
<?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>";
}
?>

Tidak ada komentar:
Posting Komentar