Update Delete pada PHP dan MySql
Projek 1 (Textfield)
koneksi.php
<?php
$koneksi = mysqli_connect("localhost","root", "", "pwpb1103");
function hapus($data){
global $koneksi;
$id = $_GET['id'];
$sql = mysqli_query($koneksi, "DELETE FROM projek1 WHERE id = '$id'");
return mysqli_affected_rows($koneksi);
}
function edit($data){
global $koneksi;
$id = $_GET['id'];
$nama = $_POST['nama'];
$sql = "UPDATE projek1 SET nama = '$nama' WHERE id ='$id'";
$query = mysqli_query($koneksi,$sql);
return mysqli_affected_rows($koneksi);
}
?>
index.php
<!DOCTYPE html>
<html>
<head>
<title>Update Delete</title>
</head>
<body>
<center>
<table border="2" cellpadding="10">
<tr>
<th>ID</th>
<th>Nama</th>
<th>Aksi</th>
</tr>
<?php
include 'koneksi.php';
$sql = mysqli_query($koneksi, "SELECT * FROM projek1");
foreach ($sql as $data) {
?>
<tr>
<td><?php echo $data["id"];?></td>
<td><?php echo $data["nama"];?></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></title>
</head>
<style type="text/css">
table{
border-radius: 13%;
border: 2px solid;
border-color: black;
margin: 30px;
}
</style>
<body>
<center>
<form method="POST">
<?php
$sql = mysqli_query($koneksi,"SELECT * FROM projek1 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 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>";
}
?>
Projek 2 (combo box)
koneksi.php
<?php
$koneksi = mysqli_connect("localhost","root", "", "pwpb1103");
function hapus($data){
global $koneksi;
$id = $_GET['id'];
$sql = mysqli_query($koneksi, "DELETE FROM projek2 WHERE id = '$id'");
return mysqli_affected_rows($koneksi);
}
function edit($data){
global $koneksi;
$id = $_GET['id'];
$nama = $_POST['nama'];
$kelas = $_POST['kelas'];
$sql = "UPDATE projek2 SET nama = '$nama', kelas = '$kelas' WHERE id ='$id'";
$query = mysqli_query($koneksi,$sql);
return mysqli_affected_rows($koneksi);
}
?>
index.php
<!DOCTYPE html>
<html>
<head>
<title>Update Delete Projek 2</title>
</head>
<body>
<center>
<table border="2" cellpadding="10">
<tr>
<th>ID</th>
<th>Nama</th>
<th>Kelas</th>
<th>Aksi</th>
</tr>
<?php
include 'koneksi.php';
$sql = mysqli_query($koneksi, "SELECT * FROM projek2");
foreach ($sql as $data) {
?>
<tr>
<td><?php echo $data["id"];?></td>
<td><?php echo $data["nama"];?></td>
<td><?php echo $data["kelas"];?></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>
<?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></title>
</head>
<style type="text/css">
table{
border-radius: 13%;
border: 2px solid;
border-color: black;
margin: 30px;
}
</style>
<body>
<center>
<form method="POST">
<?php
$sql = mysqli_query($koneksi,"SELECT * FROM projek2 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>Kelas</label></td>
<td><select name="kelas">
<option value="<?php echo $row["kelas"];?>" selected><?php echo $row["kelas"]; ?></option>
<option value="X">X</option>
<option value="XI">XI</option>
<option value="XII">XII</option>
</select></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit"></td>
</tr>
</table>
<?php } ?>
</form>
</center>
</body>
<?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>";
}
?>
Database projek 2
Projek 3 (radio button)
koneksi.php
<?php
$koneksi = mysqli_connect("localhost","root", "", "pwpb1103");
function hapus($data){
global $koneksi;
$id = $_GET['id'];
$sql = mysqli_query($koneksi, "DELETE FROM projek3 WHERE id = '$id'");
return mysqli_affected_rows($koneksi);
}
function edit($data){
global $koneksi;
$id = $_GET['id'];
$nama = $_POST['nama'];
$jenkel = $_POST['jenkel'];
$sql = "UPDATE projek3 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 projek3");
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></title>
</head>
<style type="text/css">
table{
border-radius: 13%;
border: 2px solid;
border-color: black;
margin: 30px;
}
</style>
<body>
<center>
<form method="POST">
<?php
$sql = mysqli_query($koneksi,"SELECT * FROM projek3 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>";
}
?>
Database projek 3
Projek 4 (check box)
koneksi.php
<?php
$koneksi = mysqli_connect("localhost","root", "", "pwpb1103");
function hapus($data){
global $koneksi;
$id = $_GET['id'];
$sql = mysqli_query($koneksi, "DELETE FROM projek4 WHERE id = '$id'");
return mysqli_affected_rows($koneksi);
}
function edit($data){
global $koneksi;
$id = $_GET['id'];
$nama = $_POST['nama'];
$hobi = $_POST['hobi'];
$hasil = implode(",", $hobi);
$sql = "UPDATE projek4 SET nama = '$nama', hobi = '".$hasil."' WHERE id ='$id'";
$query = mysqli_query($koneksi,$sql);
return mysqli_affected_rows($koneksi);
}
?>
index.php
<!DOCTYPE html>
<html>
<head>
<title>Update Delete Projek 4</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 projek4");
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>
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></title>
</head>
<style type="text/css">
table{
border-radius: 13%;
border: 2px solid;
border-color: black;
margin: 30px;
}
</style>
<body>
<center>
<form method="POST">
<?php
$sql = mysqli_query($koneksi,"SELECT * FROM projek4 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>Hobi</label></td>
<td><?php
$hobi = $row['hobi'];
$jarak = explode(",", $hobi);
?>
<input type="checkbox" name="hobi[]" value="Olahraga"
<?php
if (in_array("Olahraga", $jarak)) {
echo "checked";
}
?>
>
<label>Olahraga</label>
<input type="checkbox" name="hobi[]" value="Gamers"
<?php
if (in_array("Gamers", $jarak)) {
echo "checked";
}
?>
>
<label>Gamers</label>
<input type="checkbox" name="hobi[]" value="Tidur"
<?php
if (in_array("Tidur", $jarak)) {
echo "checked";
}
?>
>
<label>Tidur</label>
</td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit"></td>
</tr>
</table>
<?php } ?>
</form>
</center>
</body>
<?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>";
}
?>
Database projek 4
Komentar
Posting Komentar