Santri Indigo adalah Komunitas Santri Berbudaya Teknologi yang merupakan kerjasama program CSR PT Telkom Indonesia Tbk dengan HU Republika.
Twitter Facebook Feedburner Google +1 youtube
www.santri-indigo.com
Selamat Datang di Portal Santri Indigo Cilacap
Home » , , » Koneksi Database dengan Script PHP Part 1

Koneksi Database dengan Script PHP Part 1

Penulis : ZHANtech | Rabu, 09 April 2014

Santri-Indigo.Com - Kemarin sudah Portal Indigo Cilacap Sampaikan Bagaimana Cara Membuat Database Perpustakaan dengan Php My Admin sekarang tinggal Cara mengkoneksikan database yang sudah dibuat kemarin ke dalam script Php, contoh hasil jadinya :


Cara pembuatannya cukup rumit, makanya anda harus simak dengan baik. Caranya sebagai berikut :
  • Buat folder dengan nama perpustakaan di C:\xampp\htdocs
  • Kita buat scriptnya di dalam folder perpustakaan tersebut
  • Pertama tama script yang akan kita buat adalah script index.php (bisa dengan notepad++ ataupun notepad biasa, disimpan dengan format php)
  • Script index.php sebagai berikut
<?php
include "db_connect.php";
$query=mysql_query ("SELECT * FROM t_anggota",$connection)or die (mysql_error());
$jumlah = mysql_num_rows($query);
echo "<html>";
echo "<head>";
echo "<link href='style.css' type='text/css' rel='stylesheet'>";
echo "</head>";
echo "<title>Database Perpustakaan</title>";
echo "<body>";
echo "<font color='darkgreen' face='Tahoma' size=3><b><br>Tabel Anggota</b></font><br><br>";
echo "<a href='add.php' style=\"text-decoration: none\"><font face='tahoma' size='2'>Tambahkan Anggota Baru</font></a><br>";
echo "<br><table border=\"0\" cellpadding=\"1\" cellspacing=\"1\" bordercolor=\"blue\"
bgcolor=\"white\">
<tr bgcolor='brown' height=\"30\"><font color='white'>
<th align='left'><font color='white' face='Tahoma' size=2>Kode</font></th>
<th align='left'><font color='white' face='Tahoma' size=2>Nama</font></th>
<th align='left'><font color='white' face='Tahoma' size=2>Alamat</font></th>
<th align='left'><font color='white' face='Tahoma' size=2>No Telp</font></th>
<th align='left'><font color='white' face='Tahoma' size=2>Action</font></th>
</tr>";
$j=0;
while ($row=mysql_fetch_array($query)) {
echo "<tr><td align='left' bgcolor='#CCFF66'>";echo "<font face='Arial' size=1>";echo
$row["kd_anggota"];echo"</font>";echo"</td>";
echo "<td align='left' bgcolor='#CCFF66'>";echo "<font face='Arial' size=1>";echo
$row["nama_anggota"];echo"</font>";echo"</td>";
echo "<td align='left' bgcolor='#CCFF66'>";echo "<font face='Arial' size=1>";echo
$row["alamat_anggota"];echo"</font>";echo"</td>";
echo "<td align='left' bgcolor='#CCFF66'>";echo "<font face='Arial' size=1>";echo
$row["no_telp"];echo"</font>";echo"</td>";
echo"<td align='left' bgcolor='#CCFF66'>";echo "<a href='delete.php?kd_anggota=".$row['kd_anggota']."'
style=\"text-decoration: none\" title=\"Delete\"><font face='tahoma'
size='1'>Hapus</font></a> <a href='edit.php?kd_anggota=".$row['kd_anggota']."' style=\"text-decoration:
none\" title=\"Edit\"><font face='tahoma' size='1'>Ubah</font></a>";
$j++;
}
echo"</table>";
$query=mysql_query ("SELECT * FROM t_buku",$connection)or die (mysql_error());
$jumlah = mysql_num_rows($query);
echo "<br/><font color='darkgreen' face='Tahoma' size=3><b><br>Tabel Buku</b></font><br><br>";
echo "<a href='add2.php' style=\"text-decoration: none\"><font face='tahoma' size='2'>Tambahkan Buku Baru</font></a><br>";
echo "<br><table border=\"0\" cellpadding=\"1\" cellspacing=\"1\" bordercolor=\"blue\"
bgcolor=\"white\">
<tr bgcolor='brown' height=\"30\"><font color='white'>
<th align='left'><font color='white' face='Tahoma' size=2>Kode</font></th>
<th align='left'><font color='white' face='Tahoma' size=2>Judul Buku</font></th>
<th align='left'><font color='white' face='Tahoma' size=2>Pengarang</font></th>
<th align='left'><font color='white' face='Tahoma' size=2>Penerbit</font></th>
<th align='left'><font color='white' face='Tahoma' size=2>Tahun Terbit</font></th>
<th align='left'><font color='white' face='Tahoma' size=2>Action</font></th>
</tr>";
$j=0;
while ($row=mysql_fetch_array($query)) {
echo "<tr><td align='left' bgcolor='#CCFF66'>";echo "<font face='Arial' size=1>";echo
$row["kd_buku"];echo"</font>";echo"</td>";
echo "<td align='left' bgcolor='#CCFF66'>";echo "<font face='Arial' size=1>";echo
$row["judul_buku"];echo"</font>";echo"</td>";
echo "<td align='left' bgcolor='#CCFF66'>";echo "<font face='Arial' size=1>";echo
$row["pengarang"];echo"</font>";echo"</td>";
echo "<td align='left' bgcolor='#CCFF66'>";echo "<font face='Arial' size=1>";echo
$row["penerbit"];echo"</font>";echo"</td>";
echo "<td align='left' bgcolor='#CCFF66'>";echo "<font face='Arial' size=1>";echo
$row["tahun_terbit"];echo"</font>";echo"</td>";
echo"<td align='left' bgcolor='#CCFF66'>";echo "<a href='delete2.php?kd_buku=".$row['kd_buku']."'
style=\"text-decoration: none\" title=\"Delete2\"><font face='tahoma'
size='1'>Hapus</font></a> <a href='edit2.php?kd_buku=".$row['kd_buku']."' style=\"text-decoration:
none\" title=\"Edit2\"><font face='tahoma' size='1'>Ubah</font></a>";
$j++;
}
echo"</table>";
echo "</body>";
echo "</html>";
?>
  •  Kemudian buat script add, dan add2, contoh scriptnya sebagai berikut :

Add.php
<?php
include "db_connect.php";
$hasil = mysql_query("SELECT max(kd_anggota) as idMaks FROM t_anggota",$connection) or die (mysql_error());
$data  = mysql_fetch_array($hasil);
$kd_buku = $data['idMaks'];

//mengatur 4 karakter untuk jumlah karakter yang berubah-ubah
$noUrut = (int) substr($kd_buku, 0, 4);
$noUrut++;

//%04s untuk mengatur 4 karakter berurutan
$IDbaru = sprintf("%04s", $noUrut);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Tambahkan Anggota Baru</title>

<script type="text/javascript">
$(function() {
$( "#input" ).datepicker({
changeMonth: true,
changeYear: true
});
});
</script>
</head>
<body>
<form action="insert_anggota.php" method="POST" name="frmanggota" onsubmit="return cekform()">
<font face="Tahoma" color="green" size="3"><b><br>Tambahkan Anggota Baru</b></font><br/><br/>
<table align="left">
<tr>
<td><font face="Tahoma" color="black" size="2">Kode</font></td>
<td>:</td>
<td><input type="text" name="kd_anggota" size="40" maxlength="6" <?php echo "value='$IDbaru'"; ?> readonly="readonly" style="background:gray;" required></td>
</tr>
<tr>
<td><font face="Tahoma" color="black" size="2">Nama</font></td>
<td>:</td>
<td><input type="text" name="nama_anggota" autofocus="true" size="40" maxlength="30" required></td>
</tr>
<tr>
<td><font face="Tahoma" color="black" size="2">Tempat Lahir</font></td>
<td>:</td>
<td><input type="text" name="tempat_lahir" size="40" maxlength="20" required></td>
</tr>
<tr>
<td><font face="Tahoma" color="black" size="2">Tanggal Lahir</font></td>
<td>:</td>
<td><select name="tgl" size="1" id="tgl"><option value=''>--</option>
                <?php
             for ($i=1;$i<=31;$i++)
             {
                if($tanggal==$i) {
                    echo "<option value=".$i." selected>".$i."</option>";
                } else {
                    echo "<option value=".$i.">".$i."</option>";
                }
             }
          ?>
              </select> - 
              <select name="bln" size="1" id="bln"><option value="">--</option>
                <?php
             $namabulan=array("","Januari","Pebruari","Maret","April","Mei","Juni","Juli","Agustus","September","Oktober","November","Desember");
             for ($i=1;$i<=12;$i++)
             {
                if($bulan==$i) {
                    echo "<option value=".$i." selected>".$namabulan[$i]."</option>";
                } else {
                    echo "<option value=".$i.">".$namabulan[$i]."</option>";
                }
             }
          ?>
              </select> - 
              <select name="thn" size="1" id="thn"><option value=''>--</option>
                <?php
             for ($i=1985;$i<=2014;$i++)
             {
                if($tahun==$i) {
                    echo "<option value=".$i." selected>".$i."</option>";
                } else {
                    echo "<option value=".$i.">".$i."</option>";
                }
             }
          ?>
              </select></td>
</tr>
<tr>
<td><font face="Tahoma" color="black" size="2">Jenis Kelamin</font></td>
<td>:</td>
<td><input type="radio" name="j_kelamin" id="j_kelamin" value="Laki-Laki" required />Laki-Laki<input type="radio" name="j_kelamin" id="j_kelamin" value="Perempuan" required />Perempuan</td>
</tr>
<tr>
<td><font face="Tahoma" color="black" size="2">Alamat</font></td>
<td>:</td>
<td><textarea type="text" name="alamat_anggota" style="width:260px;" maxlength="100" required></textarea></td>
</tr>
<tr>
<td><font face="Tahoma" color="black" size="2">Penddikan Terakhir</font></td>
<td>:</td>
<td><select name="pend_terakhir" required><option value=''>--</option><option value="TK">TK</option><option value="SD/MI">SD/MI</option><option value="SMP/MTs">SMP/MTs</option><option value="SMA/MA">SMA/MA</option><option value="S1(Sarjana)">S1(Sarjana)</option><option value="S2(Sarjana)">S2(Sarjana)</option></select></td>
</tr>
<tr>
<td><font face="Tahoma" color="black" size="2">Kartu Identitas</font></td>
<td>:</td>
<td><select name="kartu_identitas" required><option value=''>--</option><option value="KTP">KTP</option><option value="SIM">SIM</option><option value="Kartu Pelajar">Kartu Pelajar</option><option value="Kartu Mahasiswa">Kartu Mahasiswa</option><option value="Paspor">Paspor</option></select></td>
</tr>
<tr>
<td><font face="Tahoma" color="black" size="2">No Identitas</font></td>
<td>:</td>
<td><input type="text" name="no_identitas" size="40" maxlength="20" required></td>
</tr>
<tr>
<td><font face="Tahoma" color="black" size="2">Agama</font></td>
<td>:</td>
<td><select name="agama"><option value=''>--</option><option value="Islam">Islam</option><option value="Kristen">Kristen<option value="Katolik">Katolik</option><option value="Hindu">Hindu</option><option value="Budha">Budha</option><option value="Lainnya">Lainnya</option></select></td>
</tr>
<tr>
<td><font face="Tahoma" color="black" size="2">Pekerjaan</font></td>
<td>:</td>
<td><input type="text" name="pekerjaan" size="40" maxlength="20" required></td>
</tr>
<tr>
<td><font face="Tahoma" color="black" size="2">No Telp</font></td>
<td>:</td>
<td><input type="text" name="no_telp" size="40" pattern="\d+" maxlength="13" required></td>
</tr>
<tr>
<td colspan="3" align="center"><br/><input type="submit" value="Tambah"> <input type="reset" value="Batal">
 <button onclick="window.location.href='data_anggota.php'">Kembali ke Daftar Anggota</button>
</td>
</tr>
</table>
</form>
</body>
</html>
Add2.php
<?php
include "db_connect.php";
$hasil = mysql_query("SELECT max(kd_buku) as idMaks FROM t_buku",$connection) or die (mysql_error());
$data  = mysql_fetch_array($hasil);
$kd_buku = $data['idMaks'];

//mengatur 1 karakter sebagai jumalh karakter yang tetap
//mengatur 4 karakter untuk jumlah karakter yang berubah-ubah
$noUrut = (int) substr($kd_buku, 1, 4);
$noUrut++;

//menjadikan B sebagai 1 karakter yang tetap
$char = "B";
//%04s untuk mengatur 4 karakter di belakang B
$IDbaru = $char . sprintf("%04s", $noUrut);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Tambahkan Buku Baru</title>
</head>
<body>
<form action="insert_buku.php" method="POST" name="frmbuku" onsubmit="return cekform()">
<font face="Tahoma" color="green" size="3"><b><br>Tambahkan Buku Baru</b></font><br/><br/>
<table align="left">
<tr>
<td><font face="Tahoma" color="black" size="2">Kode</font></td>
<td>:</td>
<td><input type="text" name="kd_buku" size="30" maxlength="6" <?php echo "value='$IDbaru'"; ?> readonly="readonly" style="background:gray;" required></td>
</tr>
<tr>
<td><font face="Tahoma" color="black" size="2">Judul Buku</font></td>
<td>:</td>
<td><input type="text" name="judul_buku" size="30"  autofocus="true" required></td>
</tr>
<tr>
<td><font face="Tahoma" color="black" size="2">Pengarang</font></td>
<td>:</td>
<td><input type="text" name="pengarang" size="30" maxlength="50" required></td>
</tr>
<tr>
<td><font face="Tahoma" color="black" size="2">Penerbit</font></td>
<td>:</td>
<td><input type="text" name="penerbit" size="30" maxlength="50" required></td>
</tr>
<tr>
<td><font face="Tahoma" color="black" size="2" pattern="\d+" maxlength="4">Tahun Terbit</font></td>
<td>:</td>
<td><input type="text" name="tahun_terbit" size="30" required></td>
</tr>
<tr>
<td><font face="Tahoma" color="black" size="2">No Klasifikasi</font></td>
<td>:</td>
<td><input type="text" name="no_klas" size="30" maxlength="7" required></td>
</tr>
<tr>
<td><font face="Tahoma" color="black" size="2">No ISBN</font></td>
<td>:</td>
<td><input type="text" name="no_isbn" size="30" maxlength="18" required></td>
</tr>
<tr>
<td><font face="Tahoma" color="black" size="2">Stok</font></td>
<td>:</td>
<td><input type="text" name="stok" size="30" maxlength="3" required></td>
</tr>
<tr>
<td colspan="3" align="center"><br/><input type="submit" value="Tambah"> <input type="reset" value="Batal">
 <button onclick="window.location.href='data_buku.php'">Kembali ke Daftar Buku</button>
</td>
</tr>
</table>
</form>
</body>
</html>
  • Saya lanjutkan di Part 2, tunggu di hari kemudian ya.
Part 1 | Part 2 | Part 3 


Share this article with your friends

1 komentar:

Unknown 22 April 2014 pukul 14.52

artikelnya menarik, aku juga pecinta PHP my SQL salam blog walking.. ditunggu kunjungan baliknya.. :)

Posting Komentar

Jangan berkunjung tanpa meninggalkan jejak.
- No Spam - No Phising - No Live Link
Salam Blogger Indonesia, Silakan Tinggalkan Pesan Agan disini... !!!

Tukar Link



Copy Paste - Copyright by SIC
<div class="separator" style="clear: both; text-align: center;">
<a href="http://www.santri-indigo.com/" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh4X8slxtycBYdn18VtsohXTzJQmwJhkNtRDtbbJv7tnt5dqou5BIn6OIdm1e3Ql3YM9ZSckUHsSXwpPacPxnw067WXUa7S3TWTDVRYOVwdV5Jwi8aLdKMMMZrX3WtykfUBqAwhWBc3DtA/s150/SI.png" /></a></div>

Bagi yang sudah pasang silahkan tinggalkan komentar