Koneksi Database PHP
Source Code koneksi PHP ke database
<?php
$host = mysqli_connect("localhost","root","","bukutamu");
?>
Source Code form tampilan utama
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>BELAJAR DATABASE PWPB</title>
</head>
<style type="text/css">
table{
border-radius: 13%;
border: 2px solid;
border-color: black;
margin: 30px;
}
</style>
<body>
<?php include 'koneksi.php' ?>
<center>
<form action="hasil_input.php" method="POST">
<table cellpadding="13">
<tr>
<th colspan="2"><h2>Input Data Tamu</h2></th>
</tr>
<tr>
<td><label>No. Tamu</label></td>
<td><input type="number" name="notamu"></td>
</tr>
<tr>
<td><label>Nama</label></td>
<td><input type="text" name="nama"></td>
</tr>
<tr>
<td><label>Email</label></td>
<td><input type="email" name="email"></td>
</tr>
<tr>
<td><label>Pesan</label></td>
<td><input type="textarea" name="pesan"></td>
</tr>
<tr align="center" colspan="2">
<td><input type="submit" name="submit"></td>
</tr>
</table>
</form>
</center>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>BELAJAR DATABASE PWPB</title>
</head>
<body>
<?php
if (isset($_POST['submit'])) {
$tamu = $_POST['notamu'];
$nama = $_POST['nama'];
$email = $_POST['email'];
$pesan = $_POST['pesan'];
include 'koneksi.php';
$hasil = mysqli_query($host, "INSERT INTO tamu VALUES ('$tamu', '$nama', '$email', '$pesan')");
}
?>
<center>
<table cellpadding="10" border="2">
<tr>
<th colspan="2"><h2>Data Anda</h2></th>
</tr>
<tr>
<td><label>No. Tamu</label></td>
<td><?php echo $tamu ?></td>
</tr>
<tr>
<td><label>Nama</label></td>
<td><?php echo $nama ?></td>
</tr>
<tr>
<td><label>Email</label></td>
<td><?php echo $email ?></td>
</tr>
<tr>
<td><label>Pesan</label></td>
<td><?php echo $pesan ?></td>
</tr>
<tr>
<td colspan="2"><a href="data_tamu.php">Lihat Seluruh Data Tamu</a></td>
</tr>
</table>
</center>
</body>
</html>
Hasil
Source Code data seluruh tamu
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>BELAJAR DATABASE PWPB</title>
</head>
<body>
<center>
<h1>DATA TAMU</h1>
<table cellpadding="10" border="2">
<tr>
<th>Nomor Tamu</th>
<th>Nama</th>
<th>Email</th>
<th>Pesan</th>
</tr>
<?php
include 'koneksi.php';
$hasil = mysqli_query($host, "SELECT * FROM tamu");
foreach ($hasil as $data) {
?>
<tr>
<td><?php echo $data["no_tamu"]; ?></td>
<td><?php echo $data["nama"]; ?></td>
<td><?php echo $data["email"]; ?></td>
<td><?php echo $data["pesan"]; ?></td>
</tr>
<?php } ?>
</table>
</center>
</body>
</html>
Hasil
Komentar
Posting Komentar