Professional Documents
Culture Documents
CRUD Using PHP MySQL
CRUD Using PHP MySQL
Gambar 3.1 Tampilan input.php berupa form entry data baru, ketika “Input Data” di klik
Gambar 3.2 Tampilan index.php setelah data baru berhasil ditambahkan
Gambar 4.1 Tampilan delete.php kotak dialog konfirmasi atas penghapusan data terpilih
Source Code
Koneksi.php
<?php
$host = "localhost";
$user = "root";
$pass = "";
$dbnm = "school";
$conn = mysql_connect ($host, $user, $pass);
if ($conn) {
$buka = mysql_select_db ($dbnm);
if (!$buka) {
die ("Database tidak dapat dibuka");
}
} else {
die ("Server MySQL tidak terhubung");
}
?>
Index.php
<!DOCTYPE>
<html>
<head>
<title>Data Alumni</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="main_container">
<div id="header">
<h1>CRUD Data Alumni</h1>
</div>
<div id="navigation">
</div>
<?php
$page = (isset($_GET['page']))? $_GET['page'] : "main";
switch ($page) {
case 'input': include "input.php"; break;
case 'edit' : include "edit.php"; break;
case 'delete' : include "delete.php"; break;
case 'main' :
default : include 'tampil.php';
}
?>
</div>
</body>
</html>
Tampil.php
<?php
include "koneksi.php";
?>
<head>
<title>DATA ALUMNI</title>
<style>
h3{
text-align:center; }
table {
border-collapse:collapse;
border-spacing:0;
font-family:Arial, sans-serif;
font-size:16px;
padding-left:300px;
margin:auto; }
table th {
font-weight:bold;
padding:10px;
color:white;
background-color:blue;
border-top:1px black solid;
border-bottom:1px black solid;}
table td {
padding:10px;
font: comicsans;
border-top:2px navy solid;
border-bottom:2px navy solid;
text-align:center; }
tr:nth-child(even) {
background-color: white; }
tr:nth-child(2n+1) {
background-color: deepskyblue; }
</style>
</head>
<div id="content">
<h3>Data Alumni</h3>
<a href="index.php?page=input"><input type="button" name=""
value=" Input Data "/></a>
<br><br>
<table width="90%" align="center" id="tabel">
<tr>
<th>No</td>
<th>NIM</td>
<th>NAMA LENGKAP</td>
<th>PRODI</td>
<th>TAHUN LULUS</td>
<th>PEKERJAAN</td>
<th>OPTIONS</td>
</tr>
<?php
$no = 1;
$query = "SELECT * FROM alumni ORDER BY nim";
$sql = mysql_query ($query);
while ($hasil = mysql_fetch_array ($sql)) {
$nim = $hasil['nim'];
$nama = stripslashes ($hasil['nama']);
$prodi = stripslashes ($hasil['prodi']);
$lulus = stripslashes ($hasil['lulus']);
$kerja = stripslashes ($hasil['kerja']);
$warna = ($no%2==1)?"#ffffff":"#efefef";
?>
<tr bgcolor="<?php echo $warna; ?>">
<td><?php echo $no; ?></td>
<td><?php echo $nim; ?></td>
<td><?php echo $nama; ?></td>
<td><?php echo $prodi; ?></td>
<td><?php echo $lulus; ?></td>
<td><?php echo $kerja; ?></td>
<td>
<a href="index.php?page=edit&nim=<?php echo $nim;
?>"><input type="button" name="" value=" Edit "/></a>
Input.php
<?php
include "koneksi.php";
if (isset($_POST['Input'])) {
$nim = addslashes (strip_tags ($_POST['nim']));
$nama = addslashes (strip_tags ($_POST['nama']));
$prodi = $_POST['prodi'];
$lulus = $_POST['lulus'];
$kerja = $_POST['kerja'];
if (strlen ($nim) != 5) {
echo"<script>alert('NIM harus 5 digit
!',document.location.href='index.php?page=input')</script>";
}
<tr>
<td width="200">NIM</td>
<td>: <input type="text" name="nim" size="18"
maxlength="5"></td>
</tr>
<tr>
<td>NAMA LENGKAP</td>
<td>: <input type="text" name="nama" size="30"
maxlength="150"></td>
</tr>
<tr>
<td>PRODI</td>
<td>: <input type="text" name="prodi" cols="40"
rows="3"></td>
</tr>
<tr>
<td width="200">TAHUN LULUS</td>
<td>: <input type="text" name="lulus" size="18"
maxlength="5"></td>
</tr>
<tr>
<td>PEKERJAAN</td>
<td>: <input type="text" name="kerja" size="30"
maxlength="150"></td>
</tr>
<tr>
<td> </td>
<td> <input type="submit" name="Input"
value=" Simpan ">
<input type="reset" name="reset" value=" Reset
">
<a href="index.php"><input type="button" name=""
value=" Kembali "/></a></td>
</tr>
</table>
</form>
</div>
Edit.php
<?php
include "koneksi.php";
if (isset($_GET['nim'])) {
$nim = $_GET['nim'];
} else {
die ("Error. No Nim Selected! ");
}
if (isset($_POST['Edit'])) {
$nim = $_POST['hnim'];
$nama = addslashes (strip_tags ($_POST['nama']));
$prodi = addslashes (strip_tags ($_POST['prodi']));
$lulus = addslashes (strip_tags ($_POST['lulus']));
$kerja = addslashes (strip_tags ($_POST['kerja']));
<tr>
<td width="170px">NIM</td>
<td width="5px">: </td>
<td> <b><?php echo $nim; ?></b></td>
</tr>
<tr>
<td>NAMA LENGKAP</td>
<td>: </td>
<td> <input type="text" name="nama"
size="30" maxlength="30" value="<?php echo $nama; ?>"></td>
</tr>
<tr>
<td>PRODI</td>
<td>: </td>
<td> <input type="text" name="prodi"
size="30" maxlength="30" value="<?php echo $prodi; ?>"></td>
</tr>
<tr>
<tr>
<td>TAHUN LULUS</td>
<td>: </td>
<td> <input type="text" name="lulus"
size="30" maxlength="30" value="<?php echo $lulus; ?>"></td>
</tr>
<tr>
<td>PEKERJAAN</td>
<td>: </td>
<td> <textarea name="kerja" cols="40"
rows="3"><?php echo $kerja; ?></textarea></td>
</tr>
<tr>
<td></td>
<td><input type="hidden" name="hnim" value="<?php
echo $nim; ?>"> </td>
<td> <input type="submit" name="Edit" value="
Simpan ">
<input type="reset" name="reset" value=" Reset
">
<a href="index.php"><input type="button"
name="" value=" Kembali "/></a></td>
<td> </td>
</tr>
</table>
</FORM>
</div>
Delete.php
<?php
include('koneksi.php');
?>