FOTO PHP
aksi.php
<?php
include 'koneksi.php';
$nama = $_POST['nama'];
$kontak = $_POST['kontak'];
$rand = rand();
$ekstensi = array('png','jpg','jpeg','gif');
$filename = $_FILES['file']['name'];
$ukuran = $_FILES['file']['size'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!in_array($ext,$ekstensi) ) {
header("location:index.php?alert=gagal_ekstensi");
}else{
if($ukuran < 2544070){
$xx = $rand.'_'.$filename;
move_uploaded_file($_FILES['file']['tmp_name'], 'file/'.$rand.'_'.$filename);
$simpan = mysqli_query($koneksi, "INSERT INTO foto VALUES('','$nama','$kontak','$xx')");
header("location:index.php?alert=berhasil");
if ($simpan) {
echo "<script>alert('File Berhasil Di Upload');
document.location.href='index.php</script>'";
}else{
echo "<script>alert('File Gagal Di Upload');
document.location.href='index.php</script>'";
}
}else{
header("location:index.php?alert=gagal_ukuran");
}
}
?>
edit.php
<?php
include 'koneksi.php';
$id = $_GET['id'];
$sql = mysqli_query($koneksi, "SELECT * FROM foto WHERE id = '$id'");
if (isset($_POST['submit'])) {
if (edit($_POST) > 0) {
echo "
<script>
alert('Gambar Berhasil Diupload');
document.location.href = 'index.php';
</script>
";
} else {
echo "
<script>
alert('Gambar Gagal Diupload');
// document.location.href = 'index.php';
</script>
";
}
}
?>
<br>
<br>
<!DOCTYPE html>
<html>
<head></head>
<style type="text/css">
table{
border-radius: 3%;
border: 3px solid;
border-color: black;
margin-top : 0px;
}
</style>
<body>
<center>
<form method="POST" enctype="multipart/form-data">
<table cellpadding="14">
<?php
foreach ($sql as $data) {
?>
<input type="number" name="id" value="<?php echo $id; ?>" hidden>
<tr>
<td colspan="2"><center><h1>Edit Data <?php echo $data['nama']; ?></h1></center></td>
</tr>
<tr>
<td><label>Nama</label></td>
<td><input type="text" name="nama" id="nama" value="<?php echo $data["nama"]; ?>"></td>
</tr>
<br>
<br>
<tr>
<td><label>Kontak</label></td>
<td><input type="number" name="kontak" id="kontak" value="<?php echo $data["kontak"]; ?>"></td>
</tr>
<tr>
<td><label>File</label></td>
<br>
<br>
<td><img src="file/<?php echo $data["foto"]; ?>" style="width: 150px;" alt="gambar">
<br>
<br>
<input type="file" name="foto" required="required" value="<?php echo $data["foto"]; ?>"></td>
</tr>
<?php } ?>
<br>
<br>
<tr>
<td colspan="2"><center> <input type="submit" name="submit" value="Edit Data">
<button><a href="index.php">Kembali</a></button>
</center></td>
</tr>
</table>
</form>
</center>
</body>
</html>
hapus.php
<?php
require 'koneksi.php';
$id = $_GET['id'];
if( hapus($id) > 0 ) {
echo "
<script>
alert('Data Berhasil Dihapus');
document.location.href = 'index.php';
</script>
";
} else {
echo "
<script>
alert('Data Gagal Dihapus')
</script>
";
}
?>
index.php
<?php
include 'koneksi.php';
?>
<!DOCTYPE html>
<html>
<head>
<title>Data Siswa</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="container">
<center>
<h2>Data Data Siswa</h2>
<?php
if(isset($_GET['alert'])){
if($_GET['alert']=='gagal_ekstensi'){
?>
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i></i> Peringatan !</h4>
Ekstensi Tidak Diperbolehkan
<?php
}elseif($_GET['alert']=="gagal_ukuran"){
?>
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i></i> Peringatan !</h4>
Ukuran File terlalu Besar
<?php
}elseif($_GET['alert']=="berhasil"){
?>
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i></i> Sukses</h4>
Berhasil Disimpan
<?php
}
}
?>
<br>
<br>
<a href="tambah.php">Tambah Data</a>
<br>
<br>
<table border="3" cellpadding="7" class="table table-bordered">
<tr>
<th>Nama</th>
<th>Kontak</th>
<th>Foto</th>
<th>Aksi</th>
</tr>
<?php
$data = mysqli_query($koneksi,"SELECT * FROM foto");
while($d = mysqli_fetch_array($data)){
?>
<tr>
<td><?php echo $d['nama']; ?></td>
<td><?php echo $d['kontak']; ?></td>
<td><img src="file/<?php echo $d['foto'] ?>" width="70" height="70"></td>
<td><button><a href="hapus.php?id=<?= $d["id"]; ?>">Hapus</a></button><br>
<button><a href="edit.php?id=<?= $d["id"]; ?>">Edit</a></button>
</td>
</tr>
<?php
}
?>
</table>
</center>
</body>
</html>
koneksi.php
<?php
$koneksi = mysqli_connect('localhost','root','','final');
function hapus ($data) {
global $koneksi;
$id = $_GET['id'];
$sql = mysqli_query($koneksi, "DELETE FROM foto WHERE id = '$id'");
return mysqli_affected_rows($koneksi);
}
function edit($data) {
global $koneksi;
$id = $_GET['id'];
$nama = $_POST['nama'];
$kontak = $_POST['kontak'];
$foto = $_POST['file'];
$sql = "UPDATE foto SET
nama = '$nama',
kontak = '$kontak'
foto = '$foto'
WHERE id = '$id'";
$query = mysqli_query($koneksi, $sql);
return mysqli_affected_rows($koneksi);
}
?>
tambah.php
<!DOCTYPE html>
<html>
<head>
<title>Tambah File</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h2>Tambah Data Siswa</h2>
<form action="aksi.php" method="post" enctype="multipart/form-data">
<label>Nama :</label>
<input type="text" name="nama" required="required">
<label>Kontak :</label>
<input type="number" name="kontak" required="required">
<label>Foto :</label>
<input type="file" name="file" required="required">
<p style="color: red">Ekstensi yang diperbolehkan .png | .jpg | .jpeg | .gif</p>
<br>
<input type="submit" name="submit" value="Simpan">
</form>
</body>
</html>
OUTPUT
Komentar
Posting Komentar