Thursday, November 3, 2022

PHP UPDATE DELETE (RADIO BUTTON)

 Koneksi.php

<?php

$koneksi = mysqli_connect("localhost","root","","siswa");

function hapus($data){

global $koneksi;

$id = $_GET['id'];

$sql = mysqli_query($koneksi, "DELETE FROM siswi WHERE id = 'id'");

return mysqli_affected_rows($koneksi);

}

function edit ($data){

global $koneksi;

$id = $_GET['id'];

$nama = $_POST['nama'];

$jk = $_POST['jk'];

$sql = "UPDATE siswi SET nama = '$nama', jk = '$jk' WHERE id = 'id'";

$query = mysqli_query($koneksi,$sql);

    return mysqli_affected_rows($koneksi);


}

?>

Index.php

<!DOCTYPE html>

<html>

<head>

<title>cilukba </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 siswi");

foreach ($sql as $data) {

?>


<tr>

<td><?php echo $data["id"];?></td>

<td><?php echo $data["nama"];?></td>

<td><?php echo $data["jk"];?></td>

<td><a href="hapus.php?id=<?=$data["id"];?>">Delete</a>

<a href="edit.php?id=<?=$data["id"];?>">Update</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 siswi WHERE id = '$id'");

  foreach ($sql as $row) {

  ?>


  <table cellpadding="13">

  <tr>

  <th colspan="2"><h2>EDIT</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  

  $jk = $row['jk'];

  if ($jk == "Laki Laki") {

  echo '<input type="radio" name="jk" value="Laki Laki" checked> Laki Laki</input>

    <input type="radio" name="jk" value="Perempuan">Perempuan';

  }elseif ($jk == "perempuan") {

  echo '<input type="radio" name="jk" value="perempuan" checked>perempuan

    <input type="radio" name="jk" 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>";

}

 ?>



Output





TUTOR PHP UPDATE DELETE

 Koneksi.php 




Index.php


<?php

require 'koneksi.php';

 ?> 

<!DOCTYPE html>

<html>

<head>

  <title>Tugas</title>

</head>

<body>

   <table border="1" cellpadding="5">

    <tr>

          <th>No</th>

          <th>Nama</th>

          <th>Aksi</th>

    </tr>

        <?php 

        $no = 1;

        $sql = mysqli_query($con, "SELECT * FROM projeklima");

        foreach ($sql as $data) {

         ?>
          <tr>

            <td><?php echo $no++; ?></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>

</body>
</html>


Hapus.php




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('Data Gagal Diubah');
</script>
";
}
}

 ?>

<!DOCTYPE html>

<html>


<head>

<title>Edit</title>

<form method="POST">

<?php 

$sql = mysqli_query($con, "SELECT * FROM projeklima WHERE id = '$id'");

foreach ($sql as $row) {

?>



  <h5 class="card-title text-center">Edit Data</h5>



  <input type="number" name="id" value="<?php echo $id; ?>" hidden>



  <input type="text" class="form-control" name="nama" id="nama" value="<?php echo $row["nama"]; ?>">



  <label for="nama">Nama</label>



  <?php } ?>



  <input type="submit" name="submit" value="Edit Data" class="btn btn-warning mb-3 fw-bold">



</div>



</form>



</div>



</div>

</div>

</body>

</html>


PHP UPDATE DELETE (RADIO BUTTON)

 Koneksi.php <?php $koneksi = mysqli_connect("localhost","root","","siswa"); function hapus($data...