#Step 1 - Merancang Database
Tahap pertama adalah membuat database mySQL dengan nama uts :
#
Step 2 – Membuat Program PHP
Setelah merancang database, sekarang kita beralih
bagaimana membuat program untuk menghasilkan seperti data diatas dengan
menggunakan php.
Pertama membuat file program baru dengan nama
koneksi.php, file ini berfungsi untuk memanggil data yang di dalam database
<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "uts";
mysql_connect($host, $user, $pass);
mysql_select_db($db);
//echo "Koneksi Terbuka";
?>
$host = "localhost";
$user = "root";
$pass = "";
$db = "uts";
mysql_connect($host, $user, $pass);
mysql_select_db($db);
//echo "Koneksi Terbuka";
?>
#
Step 3 – Menampilkan Data (Read/Select)
Tahap ketiga adalah menampilkan data dari database
dengan membuat file yang diberi nama index.php
<!DOCTYPE html>
<html>
<head>
<title> TUGAS UTS </title>
</head>
<body>
<h2> Membuat CRUD </h2>
<p><a href="index.php">HOME</a> | <a href="tambah.php">Tambah Data</a></p>
<h3> Data Kelas </h3>
<table cellpadding="8" Cellspacing ="0" border = "1">
<tr bgcolor ="#CCCCCC">
<th>No.</th>
<th>Class</th>
<th>Time</th>
<th>Day</th>
<th>Teacher</th>
<th>Start</th>
<th>Room</th>
<th>Remark</th>
<th>Option</th>
</tr>
<?php
//Include File koneksi ke database
include('koneksi.php');
$query=mysql_query("Select * from tbl_kelas order by class ASC") or die(msql_error());
if (mysql_num_rows($query)==0){
echo '<tr><td colspan="8"> Tidak ada data!</td></tr>';
}else{
$no = 1;
while ($data=mysql_fetch_assoc($query)) {
echo '<tr>';
echo '<td>'.$no.'</td>';
echo '<td>'.$data['class'].'</td>';
echo '<td>'.$data['time'].'</td>';
echo '<td>'.$data['day'].'</td>';
echo '<td>'.$data['teacher'].'</td>';
echo '<td>'.$data['start'].'</td>';
echo '<td>'.$data['room'].'</td>';
echo '<td>'.$data['remark'].'</td>';
echo '<td><a href="edit.php?id='.$data['class'].'">Edit</a> | <a href="hapus.php?id='.$data['class'].'" onclick="return confirm(\'Yakin?\')">Hapus</a></td>';
echo '</tr>';
$no++;
}
}
?>
</table>
</body>
</html>
<html>
<head>
<title> TUGAS UTS </title>
</head>
<body>
<h2> Membuat CRUD </h2>
<p><a href="index.php">HOME</a> | <a href="tambah.php">Tambah Data</a></p>
<h3> Data Kelas </h3>
<table cellpadding="8" Cellspacing ="0" border = "1">
<tr bgcolor ="#CCCCCC">
<th>No.</th>
<th>Class</th>
<th>Time</th>
<th>Day</th>
<th>Teacher</th>
<th>Start</th>
<th>Room</th>
<th>Remark</th>
<th>Option</th>
</tr>
<?php
//Include File koneksi ke database
include('koneksi.php');
$query=mysql_query("Select * from tbl_kelas order by class ASC") or die(msql_error());
if (mysql_num_rows($query)==0){
echo '<tr><td colspan="8"> Tidak ada data!</td></tr>';
}else{
$no = 1;
while ($data=mysql_fetch_assoc($query)) {
echo '<tr>';
echo '<td>'.$no.'</td>';
echo '<td>'.$data['class'].'</td>';
echo '<td>'.$data['time'].'</td>';
echo '<td>'.$data['day'].'</td>';
echo '<td>'.$data['teacher'].'</td>';
echo '<td>'.$data['start'].'</td>';
echo '<td>'.$data['room'].'</td>';
echo '<td>'.$data['remark'].'</td>';
echo '<td><a href="edit.php?id='.$data['class'].'">Edit</a> | <a href="hapus.php?id='.$data['class'].'" onclick="return confirm(\'Yakin?\')">Hapus</a></td>';
echo '</tr>';
$no++;
}
}
?>
</table>
</body>
</html>
Tampilan halaman pertama
#Step
4 – Memasukan Data (Create /Insert )
Tahap ke empat adalah memasukan data ke database
melalui form inputan yang dibuat dengan file tambah.php
<!DOCTYPE html>
<html>
<head>
<title>TUGAS UTS</title>
</head>
<body>
<h2>Membuat CRUD</h2>
<p><a href="index.php">Home</a> | <a href="tambah.php">Tambah Data</a>
</p>
<h3>Tambah Data Class</h3>
<form action="tambah-proses.php" method="post">
<table cellpadding="8" cellpadding="0">
<tr>
<td>Class</td>
<td>:</td>
<td><input type="text" name="class" required></td>
</tr>
<tr>
<td>Time</td>
<td>:</td>
<td><input type="text" name="time" required></td>
</tr>
<tr>
<td>Day</td>
<td>:</td>
<td>
<select name="day" required>
<option value="">Pilih Hari</option>
<option value="monday">Monday</option>
<option value="Tuesday">Tuesday</option>
<option value="Wednesday">Wednesday</option>
<option value="Thursday">Thursday</option>
<option value="Friday">Friday</option>
<option value="Saturday">Saturday</option>
<option value="Sunday">Sunday</option>
</select>
</td>
</tr>
<tr>
<td>Teacher</td>
<td>:</td>
<td><input type="text" name="teacher" required></td>
</tr>
<tr>
<td>Start</td>
<td>:</td>
<td><input type="text" name="start" required></td>
</tr>
<tr>
<td>Room</td>
<td>:</td>
<td><input type="text" name="room" required></td>
</tr>
<tr>
<td>Remark</td>
<td>:</td>
<td><input type="text" name="remark" required></td>
</tr>
<tr>
<td> </td>
<td></td>
<td><input type="submit" name="tambah" value="tambah"></td>
</tr>
</table>
</form>
</body>
</html>
<html>
<head>
<title>TUGAS UTS</title>
</head>
<body>
<h2>Membuat CRUD</h2>
<p><a href="index.php">Home</a> | <a href="tambah.php">Tambah Data</a>
</p>
<h3>Tambah Data Class</h3>
<form action="tambah-proses.php" method="post">
<table cellpadding="8" cellpadding="0">
<tr>
<td>Class</td>
<td>:</td>
<td><input type="text" name="class" required></td>
</tr>
<tr>
<td>Time</td>
<td>:</td>
<td><input type="text" name="time" required></td>
</tr>
<tr>
<td>Day</td>
<td>:</td>
<td>
<select name="day" required>
<option value="">Pilih Hari</option>
<option value="monday">Monday</option>
<option value="Tuesday">Tuesday</option>
<option value="Wednesday">Wednesday</option>
<option value="Thursday">Thursday</option>
<option value="Friday">Friday</option>
<option value="Saturday">Saturday</option>
<option value="Sunday">Sunday</option>
</select>
</td>
</tr>
<tr>
<td>Teacher</td>
<td>:</td>
<td><input type="text" name="teacher" required></td>
</tr>
<tr>
<td>Start</td>
<td>:</td>
<td><input type="text" name="start" required></td>
</tr>
<tr>
<td>Room</td>
<td>:</td>
<td><input type="text" name="room" required></td>
</tr>
<tr>
<td>Remark</td>
<td>:</td>
<td><input type="text" name="remark" required></td>
</tr>
<tr>
<td> </td>
<td></td>
<td><input type="submit" name="tambah" value="tambah"></td>
</tr>
</table>
</form>
</body>
</html>
Tampilan Form untuk input data :
Ketika form tersebut di isi dan di click tombol
tambah maka akan menjalankan file tambah-proses.php.
file tersebut untuk menjalankan proses penyimpanan data ke dalam database
<?php
//proses tambah data
if(isset($_POST['tambah'])) {
include('koneksi.php');
$class = $_POST['class'];
$time = $_POST['time'];
$day = $_POST['day'];
$teacher = $_POST['teacher'];
$start = $_POST['start'];
$room = $_POST['room'];
$remark = $_POST['remark'];
$input = mysql_query("Insert into tbl_kelas values('$class','$time','$day','$teacher','$start',
'$room','$remark')") or die (mysql_error());
if ($input) {
echo 'Data berhasil di tambahkan!';
echo '<a href="tambah.php">Kembali</a>';
}else{
echo 'Gagal menambahkan data!';
echo '<a href="tambah.php">Kembali</a>';
}
}else{
echo '<srcipt>window.history.back()</script>';
}
?>
//proses tambah data
if(isset($_POST['tambah'])) {
include('koneksi.php');
$class = $_POST['class'];
$time = $_POST['time'];
$day = $_POST['day'];
$teacher = $_POST['teacher'];
$start = $_POST['start'];
$room = $_POST['room'];
$remark = $_POST['remark'];
$input = mysql_query("Insert into tbl_kelas values('$class','$time','$day','$teacher','$start',
'$room','$remark')") or die (mysql_error());
if ($input) {
echo 'Data berhasil di tambahkan!';
echo '<a href="tambah.php">Kembali</a>';
}else{
echo 'Gagal menambahkan data!';
echo '<a href="tambah.php">Kembali</a>';
}
}else{
echo '<srcipt>window.history.back()</script>';
}
?>
#Step
5 – Update Data (Update/Edit)
Tahap
ini adalah melakukan edit data pada database, file yang akan di buat adalah
file edit.php.
Link
untuk menuju halaman edit adalah pada file index.php
dimana pada table kolom opsi ada link dengan nama edit dan hapus.
<html>
<head>
<title>TUGAS UTS</title>
</head>
<body>
<h2>Membuat CRUD</h2>
<p><a href="index.php">Home</a> | <a href="tambah.php">Tambah Data</a></p>
<h3>Edit Data Kelas</h3>
<?php
include('koneksi.php');
$id = $_GET['id'];
$show = mysql_query("select * from tbl_kelas where class='$id'");
if (mysql_num_rows($show)==0){
echo'<script>window.history.back()</script>';
}else{
$data = mysql_fetch_assoc($show);
}
?>
<form action="edit-proses.php" method="post">
<input type="hidden" name="id" value="<?php echo $id;?>">
<table cellpadding="8" cellspacing="0">
<tr>
<td>Class</td>
<td>:</td>
<td><input type="text" name="class" value="<?php echo $data['class']; ?>"
required></td> <!-- value diambil dari hasil query -->
</tr>
<tr>
<td>Time</td>
<td>:</td>
<td><input type="text" name="time" value="<?php echo $data['time']; ?>"
required></td>
</tr>
<tr>
<td>Day</td>
<td>:</td>
<td>
<select name="day" required>
<option value="">Pilih Hari</option>
<option value="Monday"<?php if($data['day']=='Monday'){echo 'selected';} ?>>Monday</option>
<option value="Tuesday"<?php if($data['day']=='Tuesday'){echo 'selected';} ?>>Tuesday</option>
<option value="Wednesday"<?php if($data['day']=='Wednesday'){echo 'selected';} ?>>Wednesday</option>
<option value="Thursday"<?php if($data['day']=='Thursday'){echo 'selected';} ?>>Thursday</option>
<option value="Friday"<?php if($data['day']=='Friday'){echo 'selected';} ?>>Friday</option>
<option value="Saturday"<?php if($data['day']=='Saturday'){echo 'selected';} ?>>Saturday</option>
<option value="Sunday"<?php if($data['day']=='Sunday'){echo 'selected';} ?>>Sunday</option>
</select>
</td>
</tr>
<tr>
<td>Teacher</td>
<td>:</td>
<td><input type="text" name="teacher" value="<?php echo $data['teacher']; ?>"
required></td>
</tr>
<tr>
<td>Start</td>
<td>:</td>
<td><input type="text" name="start" value="<?php echo $data['start']; ?>"
required></td>
</tr>
<tr>
<td>Room</td>
<td>:</td>
<td><input type="text" name="room" value="<?php echo $data['room']; ?>"
required></td>
</tr>
<tr>
<td>Remark</td>
<td>:</td>
<td><input type="text" name="remark" value="<?php echo $data['remark']; ?>"
required></td>
</tr>
<tr>
<td> </td>
<td></td>
<td><input type="submit" name="simpan" value="simpan"></td>
</tr>
</table>
</form>
</body>
</html>
<head>
<title>TUGAS UTS</title>
</head>
<body>
<h2>Membuat CRUD</h2>
<p><a href="index.php">Home</a> | <a href="tambah.php">Tambah Data</a></p>
<h3>Edit Data Kelas</h3>
<?php
include('koneksi.php');
$id = $_GET['id'];
$show = mysql_query("select * from tbl_kelas where class='$id'");
if (mysql_num_rows($show)==0){
echo'<script>window.history.back()</script>';
}else{
$data = mysql_fetch_assoc($show);
}
?>
<form action="edit-proses.php" method="post">
<input type="hidden" name="id" value="<?php echo $id;?>">
<table cellpadding="8" cellspacing="0">
<tr>
<td>Class</td>
<td>:</td>
<td><input type="text" name="class" value="<?php echo $data['class']; ?>"
required></td> <!-- value diambil dari hasil query -->
</tr>
<tr>
<td>Time</td>
<td>:</td>
<td><input type="text" name="time" value="<?php echo $data['time']; ?>"
required></td>
</tr>
<tr>
<td>Day</td>
<td>:</td>
<td>
<select name="day" required>
<option value="">Pilih Hari</option>
<option value="Monday"<?php if($data['day']=='Monday'){echo 'selected';} ?>>Monday</option>
<option value="Tuesday"<?php if($data['day']=='Tuesday'){echo 'selected';} ?>>Tuesday</option>
<option value="Wednesday"<?php if($data['day']=='Wednesday'){echo 'selected';} ?>>Wednesday</option>
<option value="Thursday"<?php if($data['day']=='Thursday'){echo 'selected';} ?>>Thursday</option>
<option value="Friday"<?php if($data['day']=='Friday'){echo 'selected';} ?>>Friday</option>
<option value="Saturday"<?php if($data['day']=='Saturday'){echo 'selected';} ?>>Saturday</option>
<option value="Sunday"<?php if($data['day']=='Sunday'){echo 'selected';} ?>>Sunday</option>
</select>
</td>
</tr>
<tr>
<td>Teacher</td>
<td>:</td>
<td><input type="text" name="teacher" value="<?php echo $data['teacher']; ?>"
required></td>
</tr>
<tr>
<td>Start</td>
<td>:</td>
<td><input type="text" name="start" value="<?php echo $data['start']; ?>"
required></td>
</tr>
<tr>
<td>Room</td>
<td>:</td>
<td><input type="text" name="room" value="<?php echo $data['room']; ?>"
required></td>
</tr>
<tr>
<td>Remark</td>
<td>:</td>
<td><input type="text" name="remark" value="<?php echo $data['remark']; ?>"
required></td>
</tr>
<tr>
<td> </td>
<td></td>
<td><input type="submit" name="simpan" value="simpan"></td>
</tr>
</table>
</form>
</body>
</html>
Tampilan form dari script diatas adalah seperti dibawah ini :
Form edit diatas jika di click tombol simpan maka
akan menjalan file edit-proses.php yang
berfungsi menjalankan fungsi edit / update data
<?php
if(isset($_POST['simpan'])) {
include('koneksi.php');
$id = $_POST['id'];
$time= $_POST['time'];
$day = $_POST['day'];
$teacher =$_POST['teacher'];
$start = $_POST['start'];
$room = $_POST['room'];
$remark = $_POST['remark'];
$update = mysql_query("update tbl_kelas set time='$time',day='$day',
teacher='$teacher',start='$start',room='$room',remark='$remark'
where class='$id'") or die (mysql_error());
if($update){
echo 'Data berhasil di simpan !';
echo '<a href="edit.php?id='.$id.'">Kembali</a>';
}else{
echo 'Gagal menyimpan data!';
echo '<a href="edit.php?id='.$id.'">Kembali</a>';
}
}else{
echo '<script>window.history.back()</script>';
}
?>
if(isset($_POST['simpan'])) {
include('koneksi.php');
$id = $_POST['id'];
$time= $_POST['time'];
$day = $_POST['day'];
$teacher =$_POST['teacher'];
$start = $_POST['start'];
$room = $_POST['room'];
$remark = $_POST['remark'];
$update = mysql_query("update tbl_kelas set time='$time',day='$day',
teacher='$teacher',start='$start',room='$room',remark='$remark'
where class='$id'") or die (mysql_error());
if($update){
echo 'Data berhasil di simpan !';
echo '<a href="edit.php?id='.$id.'">Kembali</a>';
}else{
echo 'Gagal menyimpan data!';
echo '<a href="edit.php?id='.$id.'">Kembali</a>';
}
}else{
echo '<script>window.history.back()</script>';
}
?>
#Step
6 – Hapus Data (Delete)
Tahap ke enam ini adalah yang terakhir yaitu
melakukan perintah delete untuk menghapus data dari database, dengan file yang
kita buat adalah hapus.php.
Link
untuk menuju halaman hapus adalah pada file index.php
dimana pada table kolom opsi ada link dengan nama edit dan hapus.
<?php
if (isset($_GET['id'])){
include('koneksi.php');
$id = $_GET['id'];
$cek = mysql_query("Select class from tbl_kelas where class='$id'") or die (mysql_error());
if (mysql_num_rows($cek)==0){
echo '<script>window.history.back()</script>';
}else{
$del=mysql_query("Delete from tbl_kelas where class ='$id'");
if ($del){
echo 'Data kelas berhasil di hapus!';
echo '<a href="index.php">Kembali</a>';
}else{
echo 'Gagal menghapus data!';
echo '<a href="index.php">Kembali</a>';
}
}
}else{
echo '<script>window.history.back()</script>';
}
?>
if (isset($_GET['id'])){
include('koneksi.php');
$id = $_GET['id'];
$cek = mysql_query("Select class from tbl_kelas where class='$id'") or die (mysql_error());
if (mysql_num_rows($cek)==0){
echo '<script>window.history.back()</script>';
}else{
$del=mysql_query("Delete from tbl_kelas where class ='$id'");
if ($del){
echo 'Data kelas berhasil di hapus!';
echo '<a href="index.php">Kembali</a>';
}else{
echo 'Gagal menghapus data!';
echo '<a href="index.php">Kembali</a>';
}
}
}else{
echo '<script>window.history.back()</script>';
}
?>
Ketika dijalankan script diatas maka terlebih dahulu akan muncul form statement apakah yakin data tersebut akan di hapus, sperti dibawah ini :
Demikian artikel yang dibuat ini sekaligus sebagai jawaban UTS mata kuliah RPL2.
Tidak ada komentar:
Posting Komentar