PART 4 UPDATE DELETE

 koneksi

<?php 

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

if (!$con) {

  echo "Koneksi Gagal";

}

function hapus ($data) {

  global $con;

  $id = $_GET['id'];

  $sql = mysqli_query($con, "DELETE FROM sacek WHERE id = '$id'");

  return mysqli_affected_rows($con);

}

function edit($data) {

  global $con;

  $id = $_GET['id'];

  $nama = $_POST['nama'];

  $hobi = $_POST['hobi'];

  $hasil = implode(",", $hobi);

  $sql = "UPDATE sacek SET

      nama = '$nama',

      hobi = ' ".$hasil." '

      WHERE id = '$id'";

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

  return mysqli_affected_rows($con);

}

 ?>


Edit


<?php 

include 'connect.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>


 </head>

 </style>


 <body>


  <center>


  <form method="POST">


  <?php 


  $sql = mysqli_query($con,"SELECT * FROM sacek 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"];

  $hoby = explode(",", $hobi);

  ?>

  <input class="form-check-input" type="checkbox" name="hobi[]" id="hobi" value="Olahraga"

  <?php 

  if (in_array("olahraga", $hoby)) {

  echo "checked";

  }

  ?>

  >

  <label class="form-check-label" for="hobi">Olahraga</label>


  <input class="form-check-input" type="checkbox" name="hobi[]" id="hobi" value="Gamers"

  <?php

  if (in_array("Gamers", $hoby)) {

  echo "checked";

  }

  ?>

  >

  <label class="form-check-label" for="hobi">Gamers</label>


  <input class="form-check-input" type="checkbox" name="hobi[]" id="hobi" value="Slebew"

  <?php

  if (in_array("Slebew", $hoby)) {

  echo "checked";

  }

  ?>

  >

  <label class="form-check-label" for="hobi">Slebew</label>

  <!-- <?php echo $row["hobi"]; ?> -->

  </td>

  </tr>

  <tr>

  <td colspan="2"><input type="submit" name="submit"></td>

  </tr>

  </table>

  <?php } ?>

  </form>

  </center>

 </body>

 </html>

hapus

<?php 

require 'connect.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>


  ";


}

 ?>


Index

<?php

require 'connect.php';

 ?> 


<!DOCTYPE html>

<html>

<head>

  <title>HALAMAN</title>

</head>

<body>

  <h1>DATA NAMA</h1>

   <table border="2">

    <tr>

          <th>No</th>

          <th>Nama</th>

          <th>Hobi</th>

          <th>Aksi</th>

    </tr>

        <?php 

        $no = 1;

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

        foreach ($sql as $data) {

         ?>


          <tr>

            <td><?php echo $no++; ?></td>

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

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

            <td>

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

              <a href="edit.php?id=<?= $data["id"]; ?>"> EDIT  </a>

            </td>

          </tr>

        <?php } ?>

      </table>

</body>


</html>










Komentar

Postingan Populer