2016 Create Read Update Delete using PHP MySql CRUD TABEL ALUMNI DWI SETIYA NINGSIH // 210 315 70 25 [COMPANY NAME] [Company address]
SS Program Gambar 1.1 Tampilan index.php Gambar 2.1 Tampilan edit.php berupa form pengeditan data
Gambar 2.2 Tampilan index.php setelah data berhasil di edit 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 Gambar 3.1 Tampilan index.php setelah data berhasil dihapus 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"> <th>no</td> <th>nim</td>
<th>nama LENGKAP</td> <th>prodi</td> <th>tahun LULUS</td> <th>pekerjaan</td> <th>options</td> <?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> <a href="index.php?page=delete&nim=<?php echo $nim; " onclick="return confirm('anda yakin akan menghapus alumni <?php echo $nama;?')"><input type="button" name="" value=" Delete "/></a></td> <?php $no++; </table> </div> 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>"; $query = "INSERT INTO alumni VALUES('$nim','$nama','$prodi','$lulus','$kerja')"; $sql = mysql_query ($query) or die (mysql_error()); if ($sql) { echo"<script>alert('data Alumni telah berhasil ditambahkan!',document.location.href='index.php')</script>"; else { echo"<script>alert('data Alumni gagal ditambahkan!',document.location.href='index.php')</script>"; <div id="content"> <h2 align="center">input Data Alumni</h2> <FORM ACTION="" METHOD="POST" NAME="input" enctype="multipart/form-data"> <table cellpadding="0" cellspacing="0" border="0" width="950"> <td width="200">nim</td> <td>: <input type="text" name="nim" size="18" maxlength="5"></td> <td>nama LENGKAP</td> <td>: <input type="text" name="nama" size="30" maxlength="150"></td> <td>prodi</td>
<td>: <input type="text" name="prodi" cols="40" rows="3"></td> <td width="200">tahun LULUS</td> <td>: <input type="text" name="lulus" size="18" maxlength="5"></td> <td>pekerjaan</td> <td>: <input type="text" name="kerja" size="30" maxlength="150"></td> <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> </table> </form> </div> Edit.php <?php include "koneksi.php"; if (isset($_get['nim'])) { $nim = $_GET['nim']; else { die ("Error. No Nim Selected! "); $query = "SELECT * FROM alumni WHERE nim='$nim'"; $sql = mysql_query ($query); $hasil = mysql_fetch_array ($sql); $nim = $hasil['nim']; $nama = stripslashes ($hasil['nama']); $prodi = stripslashes ($hasil['prodi']);
$lulus = stripslashes ($hasil['lulus']); $kerja = stripslashes ($hasil['kerja']); 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'])); $query = "UPDATE alumni SET nama='$nama',prodi='$prodi',lulus='$lulus', kerja='$kerja' WHERE nim='$nim'"; $sql = mysql_query ($query); if ($sql) { echo"<script>alert('data Alumni telah berhasil diedit!',document.location.href='index.php')</script>"; else { echo"<script>alert('data Alumni gagal diedit!',document.location.href='index.php')</script>"; <div id="content"> <h2 align="center">edit Data Alumni</h2> <FORM ACTION="" METHOD="POST" NAME="input" enctype="multipart/form-data"> <table cellpadding="0" cellspacing="0" border="0" width="950"> <td width="170px">nim</td> <td width="5px">: </td> <td> <b><?php echo $nim; </b></td> <td>nama LENGKAP</td> <td>: </td> <td> <input type="text" name="nama" size="30" maxlength="30" value="<?php echo $nama; "></td>
<td>prodi</td> <td>: </td> <td> <input type="text" name="prodi" size="30" maxlength="30" value="<?php echo $prodi; "></td> <td>tahun LULUS</td> <td>: </td> <td> <input type="text" name="lulus" size="30" maxlength="30" value="<?php echo $lulus; "></td> <td>pekerjaan</td> <td>: </td> <td> <textarea name="kerja" cols="40" rows="3"><?php echo $kerja; </textarea></td> <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> </table> </FORM> </div> Delete.php <?php include('koneksi.php'); if (isset($_get['nim']) && is_numeric($_get['nim'])) { $nim = $_GET['nim']; $result = mysql_query("delete FROM alumni WHERE nim=$nim") or die(mysql_error());
header("location: index.php"); else { header("location: index.php");