Praktek 4

 ----------------------------------------------------------------------------------------------------

index.php

<?php 

require 'koneksi.php';

if (isset($_POST['submit'])) {

if (tambah($_POST)>0){

echo "<h6 class='p-3 bg-success text-bg-success'>Proses Input Data Berhasil</h6>";

header("location: tampilan.php");

}else{

echo "<h6 class='p-3 bg-danger text-bg-danger'>Proses Input Data Gagal</h6>";

header("location: index.php");

}

}

 ?>

 <!DOCTYPE html>

 <html>

 <head>

  <meta charset="utf-8">

  <meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="css/bootstrap.css">

<link rel="stylesheet" href="css/bootstrap.min.css">

<script src="js/bootstrap.bundle.js"></script>

  <title>Praktek 4</title>

 </head>

 <body>

 <div class="m-5 p-5 bg-dark w-25 text-bg-dark rounded">

  <form method="POST">

  <label>Nama : </label>

  <input type="text" name="nama" class="input-group-text" required>

  <br>

  <label>Hobi : </label>

<br>

  <input type="checkbox" name="hoby[]" value="Olahraga">Olahraga

  <input type="checkbox" name="hoby[]" value="Gamers">Gamers

  <input type="checkbox" name="hoby[]" value="SLEBEW">SLEBEW

  <br><br>

  <input type="submit" name="submit" class="btn btn-primary fw-bold">

  <input type="reset" name="reset" class="btn btn-warning fw-bold">

<br> <br>

<a href="tampilan.php" class="btn btn-danger fw-bold">KEMBALI</a>

  </form>

</div>

 </body>

 </html>

<!-- <div class="m-5 p-5 bg-dark w-25 text-bg-dark">

    <form method="post" action="tampilan.php">

        <label> Nama : </label>

        <input type="text" name="nama" class="input-group-text" required><br>

        <label for="hoby"> Hoby : </label> <br>

        <input type="checkbox" name="hoby[]" value="Olahraga" class="">Olahraga

<input type="checkbox" name="hoby[]" value="Gamers">Gamers

<input type="checkbox" name="hoby[]" value="Slebew">Slebew

        <br><br>

        <input type="submit" value="KIRIM" name="proses" class="btn btn-primary">

        <input type="reset" value="RESET" class="btn btn-warning">

</form>

</div> -->

----------------------------------------------------------------------------------------------------

koneksi.php

<?php 

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

function tambah($data){

global $koneksi;

$nama = $_POST['nama'];

$hobi = $_POST['hoby'];

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

$query_mysql = mysqli_query($koneksi, "INSERT INTO data_3 (nama,hoby) VALUES ('$nama','$value')");

return mysqli_affected_rows($koneksi);

}

?>

----------------------------------------------------------------------------------------------------

tampilan.php

<?php 

require 'koneksi.php';

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

 ?>

 <!DOCTYPE html>

 <html>

 <head>

  <meta charset="utf-8">

  <meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="css/bootstrap.css">

<link rel="stylesheet" href="css/bootstrap.min.css">

<script src="js/bootstrap.bundle.js"></script>

  <title>Praktek 4</title>

 </head>

 <body>

 <div style="overflow-x:auto;" class="m-5">

 <h1> TAMPILKAN DATA </h1>

   <table class="table table-dark table-hover table-striped-columns text-center justify-content-center align-content-center">

<tr>   

<th>ID</th>

<th>Nama</th>

<th>Hobi</th>

</tr>

<?php 

foreach ($sql as $row) {

?>

<tr>

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

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

<td><?php echo $row ["hoby"]; ?></td>

</tr>

<?php } ?>

   </table>

   <a href="Index.php" class="btn btn-primary">Tambah Data</a>

   </div>

 </body>

 </html>

<!-- <div style="overflow-x:auto;" class="m-5">

<h1> TAMPILKAN DATA </h1>

<table class="table table-dark table-hover table-striped-columns text-center justify-content-center align-content-center">

<thead>

<th>ID</th>

<th>Nama</th>

<th>Hobi</th>

</thead>

<tbody>

</tbody>

</table>

<a href="Index.php" class="btn btn-primary">Tambah Data</a>

</div> -->

----------------------------------------------------------------------------------------------------

Hasil :





Posting Komentar

Lebih baru Lebih lama