0% found this document useful (0 votes)
14 views

Crud

Uploaded by

lufi public
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Crud

Uploaded by

lufi public
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

add.

php

<form name="form_mahasiwa" action="index.php?page=create" method="post"


enctype="multipart/form-data">
<div class="form-group">
<label for="NIM">NIM</label>
<input type="text" class="form-control" id="nim" placeholder="Nomor Indux Mahasiswa"
name="nim" required>
</div>
<div class="form-group">
<label for="Nama">Nama</label>
<input type="text" class="form-control" id="nama" placeholder="Nama" name="nama" required>
</div>
<div class="form-group">
<label for="Jurusan">Jurusan</label>
<select name="jurusan" class="form-control" id="jurusan" name="jurusan" required>
<option value="">None</option>
<option value="Manajemen Informatika">Manajemen Informatika</option>
<option value="Tehnik Informatika">Tehnik Informatika</option>
<option value="Tehnik Komputer">Tehnik Komputer</option>
<option value="Sistem Informasi">Sistem Informasi</option>
</select>
</div>
<div class="form-group">
<label for="Alamat">Alamat</label>
<textarea class="form-control" id="alamat" placeholder="Alamat" name="alamat"
required></textarea>
</div>
<div class="form-group">
<label for="Gambar">Gambar</label>
<input type="file" class="form-control" id="gambar" name="gambar" required>
</div>
<div class="form-group">
<button type="reset" class="btn btn-danger">Reset</button>
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>

create.php

<?php
$nim = $_POST['nim'];
$nama = $_POST['nama'];
$jurusan = $_POST['jurusan'];
$alamat = $_POST['alamat'];
$gambar = $_FILES['gambar']['name'];
$mysqli->query("INSERT INTO mahasiswa(nim,nama,jurusan,alamat,gambar)
VALUES('$nim','$nama','$jurusan','$alamat','$gambar')");
move_uploaded_file($_FILES['gambar']['tmp_name'],'images/'.$gambar);
header('location:index.php');
?>
delete.php

<?php
$hapus=$mysqli->query("select*from mahasiswa where id='$_GET[id]'");
// memilih gambar untuk dihapus
$nama_gambar=mysqli_fetch_array($hapus);
// nama field gambar
$lokasi=$nama_gambar['gambar'];
// alamat tempat gambar
$hapus_gambar="images/$lokasi";
// script delete gambar dari folder
unlink($hapus_gambar);
$mysqli->query("DELETE FROM mahasiswa WHERE id='$_GET[id]'");
header('location:index.php');
?>

edit.php

<?php
$edit=$mysqli->query("select*from mahasiswa where id='$_GET[id]'");
$e=mysqli_fetch_array($edit);
?>

<form name="form_mahasiwa" action="index.php?page=update" method="post"


enctype="multipart/form-data">
<div class="form-group">
<label for="NIM">NIM</label>
<input type="hidden" name="id" value="<?php echo $e['id'];?>">
<input type="text" class="form-control" id="nim" placeholder="Nomor Indux Mahasiswa"
name="nim" required value="<?php echo $e['nim'];?>">
</div>
<div class="form-group">
<label for="Nama">Nama</label>
<input type="text" class="form-control" id="nama" placeholder="Nama" name="nama" required
value="<?php echo $e['nama'];?>">
</div>
<div class="form-group">
<label for="Jurusan">Jurusan</label>
<select name="jurusan" class="form-control" id="jurusan" name="jurusan" required>
<option value="<?php echo $e['jurusan'];?>"><?php echo $e['jurusan'];?></option>
<option value="Manajemen Informatika">Manajemen Informatika</option>
<option value="Tehnik Informatika">Tehnik Informatika</option>
<option value="Tehnik Komputer">Tehnik Komputer</option>
<option value="Sistem Informasi">Sistem Informasi</option>
</select>
</div>

<div class="form-group">
<label for="Alamat">Alamat</label>
<textarea class="form-control" id="alamat" placeholder="Alamat" name="alamat" required><?php
echo $e['alamat'];?></textarea>
</div>
<div class="form-group">
<label for="Gambar">Gambar</label>
<input type="file" class="form-control" id="gambar" name="gambar">
<span><?php echo $e['gambar'];?></span>
</div>
<div class="form-group">
<button type="reset" class="btn btn-danger">Reset</button>
<button type="submit" class="btn btn-primary">Update</button>
</div>
</form>

read.php

<table class="table table-striped">


<thead>
<tr>
<th>#</th>
<th>NIM</th>
<th>Nama</th>
<th>Jurusan</th>
<th>Alamat</th>
<th>Gambar</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$no = 0;
$mahasiswa=$mysqli->query("SELECT * FROM mahasiswa");
while($m=mysqli_fetch_array($mahasiswa)){
$no++;
?>
<?php
include"paging.php";
$p = new paging_mahasiswa;
$batas = 5;
$posisi = $p->cariPosisi($batas);
$mahasiswa=$mysqli->query("SELECT * FROM mahasiswa
ORDER BY id DESC LIMIT $posisi,$batas");
$no=0;
while($m=mysqli_fetch_array($mahasiswa)){
$no++;
?>
<tr>
<th scope="row"><?php echo $no;?></th>
<td><?php echo $m['nim']; ?></td>
<td><?php echo $m['nama']; ?></td>
<td><?php echo $m['jurusan']; ?></td>
<td><?php echo $m['alamat']; ?></td>
<td><img src="images/<?php echo $m['gambar'];?>" height="50"></td>
<td>
<a href="index.php?page=edit&id=<?php echo $m['id'];?>"><i class="fa fa-pencil"></i></a> |
<a href="index.php?page=delete&id=<?php echo $m['id'];?>"><i class="fa fa-trash-
o"></i></a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<div class="halaman">
<nav aria-label="...">
<ul class="pagination">
<?php } ?>
<?php
$jmldata = mysqli_num_rows($mysqli->query("SELECT * FROM mahasiswa"));
$jmlhalaman = $p->jumlahHalaman($jmldata, $batas);
$linkHalaman = $p->navHalaman($_GET['home'], $jmlhalaman);
echo " <li class='page-item'> $linkHalaman </li>";
?>
</ul>
</nav>
</div>

update.php

<?php
$gambar = $_FILES['gambar']['name'];
// Apabila gambar tidak diganti
if (empty($gambar)){
$mysqli->query("UPDATE mahasiswa SET nim = '$_POST[nim]',
nama = '$_POST[nama]',
jurusan = '$_POST[jurusan]',
alamat = '$_POST[alamat]'
WHERE id = '$_POST[id]'");
}else{
$hapus= $mysqli->query("select*from mahasiswa where id='$_POST[id]'");
// menghapus gambar yang lama
$nama_gambar=mysqli_fetch_array($hapus);
// nama field gambar
$lokasi=$nama_gambar['gambar'];
// alamat tempat foto
$hapus_gambar="images/$lokasi";
// script untuk menghapus gambar dari folder
unlink($hapus_gambar);
move_uploaded_file($_FILES['gambar']['tmp_name'],'images/'.$gambar);
$mysqli->query("UPDATE mahasiswa SET nim = '$_POST[nim]',
nama = '$_POST[nama]',
jurusan = '$_POST[jurusan]',
alamat = '$_POST[alamat]',
gambar = '$gambar'
WHERE id = '$_POST[id]'");
}
header('location:index.php');
?>
index.php

<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags always come first -->
<title>Crud Php & Mysqli</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="font-awesome/css/font-awesome.min.css">

</head>
<body>

<div id="wrapper">

<nav class="navbar navbar-light bg-faded">


<a class="navbar-brand" href="http://aguzrybudy.com">Aguzrybudy.com</a>
<ul class="nav navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="index.php">Home<span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="index.php?page=add">Add Data</a>
</li>
</ul>
</nav>
<div class="container box">
<?php include "pages.php";?>
</div>
</div>
<!-- jQuery first, then Tether, then Bootstrap JS. -->
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.js"></script>
<script src="ckeditor/ckeditor.js"></script>
<script type="text/javascript">
CKEDITOR.replace( 'alamat',{height: 300} );
</script>
</body>
</html>
koneksi.php

<?php
$host="localhost";
$user="root";
$pass="";
$database="mahasiswa";
$mysqli=new mysqli($host,$user,$pass,$database);
if (mysqli_connect_errno()) {
trigger_error('Koneksi ke database gagal: ' .
mysqli_connect_error(), E_USER_ERROR);
}
?>

Pages.php

<?php
error_reporting( error_reporting() & ~E_NOTICE ); include "koneksi.php";
if ($_GET['page']=="add") {
include "add.php"; }
else if ($_GET['page']=="create") {
include "create.php"; }
else if ($_GET['page']=="edit") {
include "edit.php"; }
else if ($_GET['page']=="update") {
include "update.php"; }
else if ($_GET['page']=="delete") {
include "delete.php"; }
else {
include "read.php";
}
?>

Paging.php

<?php
class paging_mahasiswa{
function cariPosisi($batas){
if(empty($_GET['home'])){
$posisi=0;
$_GET['home']=1;
}
else{
$posisi = ($_GET['home']-1) * $batas;
}
return $posisi;
}
// Fungsi untuk menghitung total halaman
function jumlahHalaman($jmldata, $batas){
$jmlhalaman = ceil($jmldata/$batas);
return $jmlhalaman;
}
// Fungsi untuk link halaman 1,2,3
function navHalaman($halaman_aktif, $jmlhalaman){
$link_halaman = "<li class='page-item'></li>";
// Link ke halaman pertama (first) dan sebelumnya (prev)
if($halaman_aktif > 1){
$prev = $halaman_aktif-1;
$link_halaman .= "
<li class='page-item'><a class='page-link' href=index.php?home>First</a></li>
<li class='page-item'><a class='page-link' href=index.php?home=$prev>
Prev</a></li> ";
}
else{
$link_halaman .= " <li class='page-item disabled'><a class='page-link' href='#'
tabindex='-1' aria-label='Previous'> Previous </a></li> ";
}
// Link halaman 1,2,3, ...
$angka = ($halaman_aktif > 3 ? "<li class='page-item'> <span aria-
hidden='true'>...</span> " : " </li>");
for ($i=$halaman_aktif-2; $i<$halaman_aktif; $i++){
if ($i < 1)
continue;
$angka .= "<li class='page-item'><a class='page-link' href=index.php?
home=$i>$i</a></li> ";
}
$angka .= " <li class='page-item active'> <a class='page-link'
href='#'>$halaman_aktif</a> <span class='sr-only'>(current)</span></li> ";
for($i=$halaman_aktif+1; $i<($halaman_aktif+3); $i++){
if($i > $jmlhalaman)
break;
$angka .= "<li class='page-item'><a class='page-link' href=index.php?
home=$i>$i</a></li> ";
}
$angka .= ($halaman_aktif+2<$jmlhalaman ? " <li class='page-item'> <span
aria-hidden='true'>...</span> <a class='page-link' href=index.php?home=$jmlhalaman>
$jmlhalaman</a> " : " </li>");
$link_halaman .= "<li class='page-item'>$angka</li>";
// Link ke halaman berikutnya (Next) dan terakhir (Last)
if($halaman_aktif < $jmlhalaman){
$next = $halaman_aktif+1;
$link_halaman .= "
<li class='page-item'><a class='page-link' href=index.php?
home=$next>Next</a></li>
<li class='page-item'><a class='page-link' href=index.php?
home=$jmlhalaman>Last </a><li>";
}
else{
$prev = $halaman_aktif-1;
$link_halaman .= " <li class='page-item'><a class='page-link'
href='index.php?home=$next' aria-label='Next'> Next</a></li> ";
}
return $link_halaman;
}
}
?>

You might also like