Anda di halaman 1dari 2

1.koneksi.

php
<?php

$user_name="root";
$password="";
$database="pemerintah";
$host_name="localhost";
$koneksi=mysqli_connect($host_name,$user_name,$password,$database);
if(!$koneksi)
{
die('gagal koneksi:' .mysqli_error());
}
echo"koneksi terbuka";
mysqli_close($koneksi);

?>

2.form_presiden.html
<!DOCTYPE html>
<html>

<head>
<title>Form untuk database</title>
</head>

<body>
<br></br>
<form action="input.php" method="post">
<h3 align="left">DATA PRESIDEN</h3>
<b>Id Presiden =</b> <input type="text" name="id_p"><p></p>
<b>Nama Presiden =</b> <input type="text" name="nama_p"><p></p>
<b>Masa Jabatan =</b> <input type="text" name="masa_j"><p></p>
<b>Email =</b> <input type="text" name="email"><p></p>
<b>NO HP =</b> <input type="text" name="no_hp"><p></p>

<tr>
<td colspan="2"><input type="submit" name="submit" value="Simpan">
<input type="reset" name="Hapus" value="Hapus">
</td>
</tr>

</form>
<a href="menampilkan_data.php">Lihat Data</a>
</body>
</html>

input.php
<?php

$user_name="root";
$password="";
$database="pemerintah";
$host_name="localhost";
$koneksi=mysqli_connect($host_name,$user_name,$password,$database);
if(!$koneksi){
die ('belom konek mas bro' .mysqli_error());
}
//bacavariabel

$imk=$_POST['id_p'];
$nmk=$_POST['nama_p'];
$hmk=$_POST['masa_j'];
$amk=$_POST['email'];
$emk=$_POST['no_hp'];

$data=$imk."".$nmk."".$hmk."".$amk."".$emk."";

$sql="INSERT INTO `pemerintah`.`presiden` (`id_presiden`, `nama_presiden`,


`masa_jabatan`, `email`, `no_hp`) VALUES('$imk','$nmk', '$hmk', '$amk', '$emk')";

//eksekusi query
if(mysqli_query($koneksi,$sql)===TRUE){
echo "data $nmk telah berhasil di input\n";
}
echo"Koneksi terbuka";

mysqli_close ($koneksi);

?>

4.menampilkan_data.php
<?php

//koneksi kedalam database


$koneksi = mysqli_connect("localhost","root","","pemerintah");
if (!$koneksi){
die ('could not connect : '.mysqli_error());
}

//syntax untuk menampilkan isi dalam database

$hasil=mysqli_query($koneksi,"SELECT * FROM presiden");


$jumlah_kolom=mysqli_num_fields($hasil);
print("<br/>jumlah kolom : $jumlah_kolom <br/>");
$jumlah_baris=mysqli_num_rows($hasil);
print("jumlah baris : $jumlah_baris <br/>");
print("<table border=2 cellpadding=2 cellspacing=2>");
$n=0;
print("<tr><td>NO</td><td>ID PRESIDEN</td><td>NAMA PRESIDEN</td><td>MASA
JABATAN</td><td>EMAIL</td><td>NO HP</td></tr>");
while ($row=mysqli_fetch_row($hasil))
{
$n++;

print("<tr><td>$n</td><td>$row[0]</td><td>$row[1]</td><td>$row[2]</td><td>$row[3]</
td><td>$row[4]</td></tr>");
}
print("</table>");

mysqli_close($koneksi);

?>

Anda mungkin juga menyukai