Kamis, 03 November 2022

Projek 3 update & delete

 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>Jenis Kelamin</th>


<th>Aksi</th>


</tr>


<?php 


include 'koneksi.php';


$sql = mysqli_query($koneksi, "SELECT * FROM tugas3");




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>


Koneksi

<?php 


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




function hapus($data){


global $koneksi;




$id = $_GET['id'];




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


return mysqli_affected_rows($koneksi);


}




function edit($data){


global $koneksi;




$id = $_GET['id'];


$nama = $_POST['nama'];


$jenkel = $_POST['jenkel'];




$sql = "UPDATE tugas3 SET nama = '$nama', jenkel = '$jenkel' WHERE id ='$id'";


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


return mysqli_affected_rows($koneksi);


}


 ?>



Edit 

<?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 tugas3 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 


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