Project 4
koneksi.php
<?php
$koneksi = mysqli_connect("localhost","root","","project4") ;
function tambah($data){
global $koneksi;
$nama = $_POST['nama'];
$hobi = $_POST['hobi'];
$value = implode(",", $hobi);
$query = mysqli_query($koneksi, "INSERT INTO projectempat (nama,hobi) VALUES ('$nama','$value')");
return mysqli_affected_rows($koneksi);
}
?>
form.php
<?php
require 'koneksi.php';
if (isset($_POST['submit'])) {
if (tambah($_POST)>0){
header("location: hasil.php");
}else{
echo "gagal";
}
}
?>
<!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>Nama : </label>
<input type="text" name="nama">
<br>
<label>Hobi : </label>
<input type="checkbox" name="hobi[]" value="Olahraga">Olahraga
<input type="checkbox" name="hobi[]" value="Gamers">Gamers
<input type="checkbox" name="hobi[]" value="Tidur">Tidur
<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 projectempat 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>
<table border="2">
<tr>
<th>Nama</th>
<th>Hobi</th>
</tr>
<?php
foreach ($sql as $row) {
?>
<tr>
<td><?php echo $row ["nama"]; ?></td>
<td><?php echo $row ["hobi"]; ?></td>
</tr>
<?php } ?>
</table>
</body>
</html>
Komentar
Posting Komentar