MEMBUAT APLIKASI DARI DASAR HINGGA AKHIR
PRAKTEK 1
SOURCE CODE
koneksi.php
<?php
$koneksi = mysqli_connect("localhost", "root", "", "pwdpb");
function tambah($data){
global $koneksi;
$nama = $_POST['nama'];
$query = mysqli_query($koneksi, "INSERT INTO praktek1 (nama) VALUES ('$nama')");
return mysqli_affected_rows($koneksi);
}
?>
form_nama.php
<?php
require 'koneksi.php';
if (isset($_POST['submit'])) {
if (tambah($_POST) > 0) {
echo "
<script>
alert('Data Berhasil Ditambahkan');
document.location.href = 'hasil_nama.php';
</script>
";
} else {
echo "
<script>
alert('Data Gagal Ditambahkan');
document.location.href = 'form_nama.php';
</script>
";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>PRAKTEK 1</title>
</head>
<body>
<form method="POST">
<label for="nama">Masukkan Nama : </label>
<input type="text" name="nama">
<br>
<input type="submit" name="submit" value="KIRIM!">
<input type="reset" name="reset" value="RESET">
</form>
</body>
</html>
hasil_nama.php
<?php
require 'koneksi.php';
$query = mysqli_query($koneksi, "SELECT * FROM praktek1 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">
<thead>
<th>Hasil Data</th>
</thead>
<?php
foreach ($query as $row) {
?>
<tbody>
<tr>
<td>Nama : </td>
<td><?php echo $row["nama"]; ?></td>
</tr>
</tbody>
<?php } ?>
</table>
</body>
</html>
TAMPILAN
.png)
.png)
Komentar
Posting Komentar