membuat database DI PHP
MEMPERAKTIKKAN DATABASE DI PHP
KONEK.PHP
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dimas";
$conn = mysqli_connect($servername,$username,$password,$dbname);
if(!$conn){
die("Koneksi Gagal : " .mysqli_connect_error($conn));
}
?>
INDEX.PHP
<?php
require 'konek.php';
$tampil = "SELECT * FROM pbo";
$result = mysqli_query($conn,$tampil);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>TAMPILAN</h1>
<h4><a href="form.php">TAMBAH DATA</a></h4>
<table border="1">
<tr>
<th>NIS</th>
<th>Nama Lengkap</th>
<th>Nama khas</th>
<th>Jenis kelamin</th>
<th>Tempat Lahir</th>
<th>Tanggal Lahir</th>
<th>Kelas</th>
<th>Jurusan</th>
<th>Hobby</th>
</tr>
<?php
while($row = mysqli_fetch_assoc($result)){
$nis = $row['nis'];
$namalengkap = $row['namalengkap'];
$namakhas = $row['namakhas'];
$jeniskelamin = $row['jeniskelamin'];
$tempatlahir = $row['tempatlahir'];
$tanggallahir = $row['tanggallahir'];
$kelas = $row['kelas'];
$jurusan = $row['jurusan'];
$hobby = $row['hobby'];
?>
<?php $i = 1; ?>
<tr>
<td><?php echo $nis ?></td>
<td><?php echo $namalengkap ?></td>
<td><?php echo $namakhas ?></td>
<td><?php echo $jeniskelamin ?></td>
<td><?php echo $tempatlahir ?></td>
<td><?php echo $tanggallahir ?></td>
<td><?php echo $kelas ?></td>
<td><?php echo $jurusan ?></td>
<td><?php echo $hobby ?></td>
</tr>
<?php $i++ ?>
<?php } ?>
</table>
</body>
</html>
FORM.PHP
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="input.php" method="POST" enctype="multipart/form-data">
<table border="2" cellpadding="5">
<th colspan="2">FORM INPUT DATA</th>
<tr>
<td>NIS</td>
<td><input type="number" name="nis"></td>
</tr>
<tr>
<td>Nama Lengkap</td>
<td><input type="text" name="namalengkap"></td>
</tr>
<tr>
<td>Nama Khas</td>
<td><input type="text" name="namakhas"></td>
</tr>
<tr>
<td>jenis kelamin</td>
<td><input type="text" name="jeniskelamin"></td>
</tr>
<tr>
<td>Tempat Lahir</td>
<td><input type="text" name="tempatlahir"></td>
</tr>
<tr>
<td>Tanggal Lahir</td>
<td><input type="date" name="tanggallahir"></td>
</tr>
<tr>
<td>Kelas</td>
<td><select name="kelas" id="">
<option value="X">X</option>
<option value="XI">XI</option>
<option value="XII">XII</option>
</select></td>
</tr>
<tr>
<td>Jurusan</td>
<td><input type="text" name="jurusan"></td>
</tr>
<tr>
<td>Hobi</td>
<td><input type="text" name="hobi"></td>
</tr>
<tr>
<th colspan="2"><input type="submit" name="kirim"></th>
</tr>
</table>
</form>
</body>
</html>
INPUT.PHP
<?php
require 'konek.php';
if(isset($_POST['kirim'])){
$nis = $_POST['nis'];
$namalengkap = $_POST['namalengkap'];
$namakhas = $_POST['namakhas'];
$jeniskelamin = $_POST['jeniskelamin'];
$tempatlahir = $_POST['tempatlahir'];
$tanggallahir = $_POST['tanggallahir'];
$kelas = $_POST['kelas'];
$jurusan = $_POST['jurusan'];
$hobi = $_POST['hobi'];
$input = "INSERT INTO pbo(nis,namalengkap,namakhas,jeniskelamin,tempatlahir,tanggallahir,kelas,jurusan,hobby) VALUES ('$nis','$namalengkap','$namakhas','$jeniskelamin','$tempatlahir','$tanggallahir','$kelas','$jurusan','$hobi')";
$hasil = mysqli_query($conn, $input);
if ($hasil){
header("location:index.php");
}else {
echo "GAGAL";
}
}else {
header("location:index.php");
}
HASIL&TAMPILAN
Komentar
Posting Komentar