PART4
KONEKSI
<?php
$koneksi = mysqli_connect("localhost", "root", "", "partempat");
function tambah($data){
global $koneksi;
$nama = $_POST['nama'];
$hobi = $_POST['hobi'];
$value = implode(",", $hobi);
$query = mysqli_query($koneksi, "INSERT INTO part (nama, hobi) VALUES ('$nama', '".$value."')");
return mysqli_affected_rows($koneksi);
}
?>
FORMULIR
<?php
require'konekkin.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 = 'form1.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
require 'konekkin.php';
$sql = mysqli_query($koneksi, "SELECT * FROM part 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>
Komentar
Posting Komentar