Selasa, 01 November 2022

MEMBUAT APK DARI DASAR PART 4

 PRAKTEK 4


source code file "koneksi5.php"

<?php  

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

function tambah($data){
global $koneksi;

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

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

return mysqli_affected_rows($koneksi);
}

?>


source code file "input5.php"

<?php 

require'koneksi5.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 = 'input5.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>


source code file "hasil.php"

<?php 

require 'koneksi5.php';

$sql = mysqli_query($koneksi, "SELECT * FROM projek4 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>


HASIL OUTPUT:




Tidak ada komentar:

Posting Komentar

Menambahkan Edit dan Delete pada PHP

 PRAKTIK 1 source code file "koneksi1.php" <?php  $host = mysqli_connect("localhost","root","",...