MEMBUAT APLIKASI DARI DASAR HINGGA AKHIR (PRAKTIK EMPAT)

 SOURCE CODE

koneksi.php

<?php  

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

function tambah($data){
global $koneksi;

$nama = $_POST['nama'];
$hobi = $_POST['hobi'];

$value = implode(",", $hobi);
$query = mysqli_query($koneksi, "INSERT INTO praktek4 (nama, hobi) VALUES ('$nama', '".$value."')");

return mysqli_affected_rows($koneksi);
}

?>

form.php

<?php 

require'koneksi.php';

if (isset($_POST['submit'])) {
if (tambah($_POST) > 0) {
echo "
<script>
alert('Data Berhasil Ditambahkan');
document.location.href = 'hasil.php';
</script>
";
} else {
echo "
<script>
alert('Data Gagal Ditambahkan');
document.location.href = 'form.php';
</script>
";
}
}

 ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
</head>
<body>
<form method="POST">
<label for="nama">Nama : </label>
<input type="text" name="nama">
<br>
<label for="hobi">Hobi : </label>
<input type="checkbox" name="hobi[]" value="Olahraga">Olahraga
<input type="checkbox" name="hobi[]" value="Gamers">Gamers
<input type="checkbox" name="hobi[]" value="Slebew">Slebew
<br> 
<input type="submit" name="submit">
<input type="reset" name="reset">
</form>
</body>
</html>

hasil.php

<?php 

require 'koneksi.php';

$sql = mysqli_query($koneksi, "SELECT * FROM praktek4 ORDER BY id DESC LIMIT 1");

 ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
</head>
<body>
<h1>HASIL DATA</h1>
<table border="2">
<thead>
<th>Nama</th>
<th>Hobi</th>
</thead>

<?php 

while ($row = mysqli_fetch_array($sql)) {

?>

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

<?php } ?>
</table>
</body>
</html>

TAMPILAN





Komentar

Postingan populer dari blog ini

Membuat Tampilan Seperti Telegram Dengan HTML & CSS