Santri-Indigo.Com - Okey, sesuai janji pada hari kemarin, kita akan lanjutkan belajar kita Cara Koneksi Database dengan Script PHP,
Pada Koneksi Database dengan Script PHP Part 1 kita telah membuat index.php, add.php, dan add2.php. Yuck kita lanjutkan, simak baik - baik ya.
- Sekarang buat db_connect.php, scriptnya sebagai berikut :
<?php
$host = "localhost";
$username = "root";
$password = "";
$databasename = "db_perpustakaan";
$connection = mysql_connect($host, $username, $password) or die("Kesalahan Koneksi ... !!");
mysql_select_db($databasename, $connection) or die("Databasenya Error");
?>
- kemudian script delete.php dan delete2.php
delete.php
<?php
include "db_connect.php";
$kd_anggota = $_GET['kd_anggota'];
$query = "DELETE FROM t_anggota WHERE kd_anggota = '$kd_anggota'";
mysql_query($query);
$q = "SELECT * from t_pinjam where kd_anggota like '$kd_anggota' "; //query to get the search result
$result = mysql_query($q);
while ($row = mysql_fetch_array($result)) {
$judul_buku = ($row['judul_buku']);
$jumlah = ($row['jumlah']);
$q2 = "SELECT * from t_buku where judul_buku like '$judul_buku' "; //query to get the search result
$result2 = mysql_query($q2);
while ($data = mysql_fetch_array($result2)) {
$kd_buku = ($data['kd_buku']);
$update = ($data['stok']+$jumlah);
$q3=mysql_query ("UPDATE t_buku SET kd_buku='$kd_buku', stok='$update' WHERE
kd_buku='$kd_buku'",$connection)or die (mysql_error());
}
}
$query2 = "DELETE FROM t_pinjam WHERE kd_anggota = '$kd_anggota'";
mysql_query($query2);
?>
<script language="JavaScript">
document.location='data_anggota.php'</script>
delete2.php
<?php
include "db_connect.php";
$kd_buku = $_GET['kd_buku'];
$q = "SELECT * from t_buku where kd_buku like '$kd_buku' "; //query to get the search result
$result = mysql_query($q);
while ($data = mysql_fetch_array($result)) {
$judul_buku = ($data['judul_buku']);
$q2=mysql_query ("DELETE FROM t_pinjam WHERE judul_buku = '$judul_buku'",$connection)or die (mysql_error());
}
$query = "DELETE FROM t_buku WHERE kd_buku = '$kd_buku'";
mysql_query($query);
?>
<script language="JavaScript">
document.location='data_buku.php'</script>
- Ditambah lagi edit.php dan edit2.php
edit.php
<?php
include "db_connect.php";
$kd_anggota = $_GET['kd_anggota'];
$query=mysql_query ("SELECT * FROM t_anggota WHERE kd_anggota='$kd_anggota'");
while ($row=mysql_fetch_array($query)){
$kd_anggota = $row['kd_anggota'];
$tgl_daftar = explode("-",$row['tgl_daftar']);
$tgl_daftar_tgl = intval($tgl_daftar[2]);
$tgl_daftar_bln = intval($tgl_daftar[1]);
$tgl_daftar_thn = intval($tgl_daftar[0]);
$nama_anggota = $row['nama_anggota'];
$tempat_lahir = $row['tempat_lahir'];
$j_kelamin = $row['j_kelamin'];
$tgl_lahir = explode("-",$row['tgl_lahir']);
$tgl_lahir_tgl = intval($tgl_lahir[2]);
$tgl_lahir_bln = intval($tgl_lahir[1]);
$tgl_lahir_thn = intval($tgl_lahir[0]);
$pend_terakhir = $row['pend_terakhir'];
$alamat_anggota = $row['alamat_anggota'];
$kartu_identitas = $row['kartu_identitas'];
$no_identitas = $row['no_identitas'];
$agama = $row['agama'];
$pekerjaan= $row['pekerjaan'];
$no_telp = $row['no_telp'];
echo "<html>";
echo "<body>";
echo "<font face='tahoma' color='green' size=4><b>Ubah Data Anggota</b></font><br/>";
echo "<table align='left'>";
echo "<form method=\"post\" action=\"update.php?kd_anggota=$kd_anggota\" enctype='multipart/form-data'>";
echo "<br/>";
echo "<tr><td><font face='Tahoma' color='black' size=2>Kode
</font></td><td>:</td><td><input type='text' name='kd_anggota' value='$kd_anggota' readonly='readonly' style='background:gray;'
size='40' maxlength='4'> </td></tr>";
echo "<tr><td><font face='Tahoma' color='black' size=2>Tanggal Pendaftaran
</font></td><td>:</td><td>";
?>
<select name="tgl_daftar_tgl" required><option value="">--</option>
<?php
for ($g = 1; $g <= 31; $g++) {
if ($g == $tgl_daftar_tgl) {
echo '<option value="'.$g.'" selected>'.$g.'</option>';
} else {
echo '<option value="'.$g.'">'.$g.'</option>';
}
}
?></select> - <select name="tgl_daftar_bln" required><option value="">--</option>
<?php
$namabulan=array("","Januari","Pebruari","Maret","April","Mei","Juni","Juli","Agustus","September","Oktober","November","Desember");
for ($h = 1; $h <= 12; $h++) {
if ($h == $tgl_daftar_bln) {
echo '<option value="'.$h.'" selected>'.$namabulan[$h].'</option>';
} else {
echo '<option value="'.$h.'">'.$namabulan[$h].'</option>';
}
}
?></select> - <select name="tgl_daftar_thn" required><option value="">--</option>
<?php
for ($j = 1985; $j <= 2015; $j++) {
if ($j == $tgl_daftar_thn) {
echo '<option value="'.$j.'" selected>'.$j.'</option>';
} else {
echo '<option value="'.$j.'">'.$j.'</option>';
}
}
?></select> </td></tr>
<?php
echo "<tr><td><font face='Tahoma' color='black' size=2>Nama
</font></td><td>:</td><td><input type='text' name='nama_anggota' value='$nama_anggota'
size='40' maxlength='30'> </td></tr>";
echo "<tr><td><font face='Tahoma' color='black' size=2>Tempat Lahir
</font></td><td>:</td><td><input type='text' name='tempat_lahir' value='$tempat_lahir'
size='40' maxlength='20'> </td></tr>";
echo "<tr><td><font face='Tahoma' color='black' size=2>Tanggal Lahir
</font></td><td>:</td><td>";
?>
<select name="tgl" required><option value="">--</option>
<?php
for ($b = 1; $b <= 31; $b++) {
if ($b == $tgl_lahir_tgl) {
echo '<option value="'.$b.'" selected>'.$b.'</option>';
} else {
echo '<option value="'.$b.'">'.$b.'</option>';
}
}
?></select> - <select name="bln" required><option value="">--</option>
<?php
$namabulan=array("","Januari","Pebruari","Maret","April","Mei","Juni","Juli","Agustus","September","Oktober","November","Desember");
for ($c = 1; $c <= 12; $c++) {
if ($c == $tgl_lahir_bln) {
echo '<option value="'.$c.'" selected>'.$namabulan[$c].'</option>';
} else {
echo '<option value="'.$c.'">'.$namabulan[$c].'</option>';
}
}
?></select> - <select name="thn" required><option value="">--</option>
<?php
for ($x = 1985; $x <= 2014; $x++) {
if ($x == $tgl_lahir_thn) {
echo '<option value="'.$x.'" selected>'.$x.'</option>';
} else {
echo '<option value="'.$x.'">'.$x.'</option>';
}
}
?></select> </td></tr>
<?php
echo "<tr><td><font face='Tahoma' color='black' size=2>Jenis Kelamin
</font></td><td>:</td><td>";
$pil_jk = array('Laki-Laki','Perempuan');
for ($a = 0; $a < sizeof($pil_jk); $a++) {
if ($j_kelamin == $pil_jk[$a]) {
echo '<label style="width: 25px; display: inline"><input type="radio" name="j_kelamin" required value="'.$pil_jk[$a].'" checked> '.$pil_jk[$a].' </label>';
} else {
echo '<label style="width: 25px; display: inline"><input type="radio" name="j_kelamin" required value="'.$pil_jk[$a].'"> '.$pil_jk[$a].' </label>';
}
}
echo "</td></tr>";
echo "<tr><td><font face='Tahoma' color='black' size=2>Alamat
</font></td><td>:</td><td><textarea type='text' name='alamat_anggota' value='$alamat_anggota'
style='width:260' maxlength='100'>$alamat_anggota</textarea> </td></tr>";
echo "<tr><td><font face='Tahoma' color='black' size=2>Pendidikan Terakhir
</font></td><td>:</td><td><select name='pend_terakhir'><option value=''>--</option>";
$pil_pendidikan = array('SD/MI','SMP/MTs','SMA/MA','S1(Sarjana)','S2(Sarjana)','Tidak Ada');
for ($f = 0; $f < sizeof($pil_pendidikan); $f++) {
if ($pend_terakhir == $pil_pendidikan[$f]) {
echo '<option value="'.$pil_pendidikan[$f].'" selected>'.$pil_pendidikan[$f].'</option>';
} else {
echo '<option value="'.$pil_pendidikan[$f].'">'.$pil_pendidikan[$f].'</option>';
}
}
echo "</select> </td></tr>";
echo "<tr><td><font face='Tahoma' color='black' size=2>Kartu Identitas
</font></td><td>:</td><td><select name='kartu_identitas'><option value=''>--</option>";
$pil_identitas = array('KTP','SIM','Kartu Pelajar','Kartu Mahasiswa','Paspor');
for ($d = 0; $d < sizeof($pil_identitas); $d++) {
if ($kartu_identitas == $pil_identitas[$d]) {
echo '<option value="'.$pil_identitas[$d].'" selected>'.$pil_identitas[$d].'</option>';
} else {
echo '<option value="'.$pil_identitas[$d].'">'.$pil_identitas[$d].'</option>';
}
}
echo "</select> </td></tr>";
echo "<tr><td><font face='Tahoma' color='black' size=2>No Identitas
</font></td><td>:</td><td><input type='text' name='no_identitas' value='$no_identitas'
size='40' maxlength='20'> </td></tr>";
echo "<tr><td><font face='Tahoma' color='black' size=2>Agama
</font></td><td>:</td><td><select name='agama'><option value=''>--</option>";
$pil_agama = array('Islam','Kristen','Katolik','Hindu','Budha','Lainnya');
for ($e = 0; $e < sizeof($pil_agama); $e++) {
if ($agama == $pil_agama[$e]) {
echo '<option value="'.$pil_agama[$e].'" selected>'.$pil_agama[$e].'</option>';
} else {
echo '<option value="'.$pil_agama[$e].'">'.$pil_agama[$e].'</option>';
}
}
echo "</select> </td></tr>";
echo "<tr><td><font face='Tahoma' color='black' size=2>Pekerjaan
</font></td><td>:</td><td><input type='text' name='pekerjaan' value='$pekerjaan'
size='40' maxlength='20'> </td></tr>";
echo "<tr><td><font face='Tahoma' color='black' size=2>No Telp
</font></td><td>:</td><td><input type='text' name='no_telp' value='$no_telp'
size='40' pattern='\d+' maxlength='13'> </td></tr>";
echo "<td colspan='3' align='center'><br/><font size='2'><input type='submit' name='submit'
value='Ubah'/> </font> <button onclick=\"window.location.href='data_anggota.php'\">Kembali ke Daftar Anggota</button></td></tr>";
echo "</table></form></body></html>";
}
?>
edit2.php
<?php
include "db_connect.php";
$kd_buku = $_GET['kd_buku'];
$query=mysql_query ("SELECT * FROM t_buku WHERE kd_buku='$kd_buku'");
while ($row=mysql_fetch_array($query)){
$kd_buku = $row['kd_buku'];
$judul_buku = $row['judul_buku'];
$pengarang = $row['pengarang'];
$penerbit = $row['penerbit'];
$tahun_terbit = $row['tahun_terbit'];
$no_klas = $row['no_klas'];
$no_isbn = $row['no_isbn'];
$stok = $row['stok'];
echo "<html>";
echo "<body>";
echo "<font face='tahoma' color='green' size=4><b>Ubah Data Buku</b></font><br/>";
echo "<table align='left'>";
echo "<form method=\"post\" action=\"update2.php?kd_buku=$kd_buku\" enctype='multipart/form-data'>";
echo "<br>";
echo "<tr><td><font face='Tahoma' color='black' size=2>Kode
</font></td><td>:</td><td><input type='text' name='kd_buku' value='$kd_buku' readonly='readonly' style='background:gray;'
size='30' maxlength='6'> </td></tr>";
echo "<tr><td><font face='Tahoma' color='black' size=2>Judul Buku
</font></td><td>:</td><td><input type='text' autofocus='true' name='judul_buku' value='$judul_buku'
size='30'> </td></tr>";
echo "<tr><td><font face='Tahoma' color='black' size=2>Pengarang
</font></td><td>:</td><td><input type='text' name='pengarang' value='$pengarang'
size='30' maxlength='50'> </td></tr>";
echo "<tr><td><font face='Tahoma' color='black' size=2>Penerbit
</font></td><td>:</td><td><input type='text' name='penerbit' value='$penerbit'
size='30' maxlength='50'> </td></tr>";
echo "<tr><td><font face='Tahoma' color='black' size=2>Tahun Terbit
</font></td><td>:</td><td><input type='text' name='tahun_terbit' value='$tahun_terbit'
size='30' pattern='\d+' maxlength='4'> </td></tr>";
echo "<tr><td><font face='Tahoma' color='black' size=2>No Klasifikasi
</font></td><td>:</td><td><input type='text' name='no_klas' value='$no_klas'
size='30' maxlength='7'> </td></tr>";
echo "<tr><td><font face='Tahoma' color='black' size=2>No ISBN
</font></td><td>:</td><td><input type='text' name='no_isbn' value='$no_isbn'
size='30' maxlength='18'> </td></tr>";
echo "<tr><td><font face='Tahoma' color='black' size=2>Stok
</font></td><td>:</td><td><input type='text' name='stok' value='$stok'
size='30' maxlength='3'> </td></tr>";
echo "<tr><td colspan='3' align='center'><br/><font size='2'><input type='submit' name='submit'
value='Ubah'/></font> <button onclick=\"window.location.href='data_buku.php'\">Kembali ke Daftar Buku</button></td></tr>";
echo "</table></form></body></html>";
}
?>
- Sudah dulu ya, lanjut di part berikutnya
Share this article with your friends
Posting Komentar
Jangan berkunjung tanpa meninggalkan jejak.
- No Spam - No Phising - No Live Link
Salam Blogger Indonesia, Silakan Tinggalkan Pesan Agan disini... !!!