Menghubungkan Database MSQL dengan PHP Menggunakan Bootstrap dan JQuery

 Membuat Database dengan nama php8 berikut strukturnya :

id_gambar [Sebagai Primary key dan Auto Increment]
gambar [Sebagai tempat untuk menampung file]

Kemudian koneksikan database dengan membuat folder database.php

Source Code: database.php < Untuk Koneksikan database

<?php
    $host="localhost";
    $user="root";
    $password="";
    $db="php8";
   
    $kon = mysqli_connect($host,$user,$password,$db);
    if (!$kon){
          die("Koneksi gagal:".mysqli_connect_error());
    }
?>

Source Code : index.php

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Upload File Gambar di PHP dan MySQL</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,
    initial-scale=1">
    <link rel="stylesheet" href="bootstrap/css/bootstrap
    .min.css">
    <script src="bootstrap/js/bootstrap.min.js"></script>
    <script src="js/jquery-3.4.1.js"></script>
</head>

<body>
<?php require("upload.php")?>
</body>
</html>

<style>
    .file {
    visibility: hidden;
    position: absolute;
    }
</style>

<script>

    function konfirmasi(){
        konfirmasi=confirm("Apakah anda yakin ingin
        menghapus gambar ini?")
        document.writeln(konfirmasi)
    }

    $(document).on("click", "#pilih_gambar", function() {
    var file = $(this).parents().find(".file");
    file.trigger("click");
    });

    $('input[type="file"]').change(function(e) {
    var fileName = e.target.files[0].name;
    $("#file").val(fileName);

    var reader = new FileReader();
    reader.onload = function(e) {
        // get loaded data and render thumbnail.
        document.getElementById("preview").src =
        e.target.result;
    };
    // read the image file as a data URL.
    reader.readAsDataURL(this.files[0]);
    });
</script>

Source Code : upload.php

<br>
    <div class="container">
        <?php
        //Validasi untuk menampilkan pesan pemberitahuan
        if (isset($_GET['add'])) {
     
            if ($_GET['add']=='berhasil'){
                echo"<div class='alert alert-success'>
            <strong>Berhasil!</strong>
            File gambar telah diupload!</div>";
            }else if ($_GET['add']=='gagal'){
                echo"<div class='alert alert-danger'>
            <strong>Gagal!</strong>
            File gambar gagal diupload!</div>";
            }    
        }

        if (isset($_GET['hapus'])) {
   
            if ($_GET['hapus']=='berhasil'){
                echo"<div class='alert alert-success'>
                <strong>Berhasil!</strong>
                File gambar telah dihapus!</div>";
            }else if ($_GET['hapus']=='gagal'){
                echo"<div class='alert alert-danger'>
                <strong>Gagal!</strong>
                File gambar gagal dihapus!</div>";
            }    
        }
        ?>
        <form action="simpan.php" method="post"
            enctype="multipart/form-data">
            <!-- rows -->  
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <div id="msg"></div>
                        <input type="file" name="gambar"     
                        class="file" >
                            <div class="input-group my-3">
                                <input type="text"
                                class="form-control"
                                 disabled
                     placeholder="Upload Gambar" id="file">
                                <div class="input-group-
                                append">
                                        <button type=
                                  "button"
                                    id="pilih_gambar"    
           class="browse btn btn-dark">Pilih Gambar</button>
                                </div>
                            </div>
                        <img src="gambar/80x80.png"
                        id="preview" class="img-thumbnail">
                    </div>
                </div>
            </div>

                <button type="submit" name="btn_simpan"
                class="btn btn-success mt-3">Simpan</button>
        </form>

        <hr>
        <div class="row center">
            <div class="col-sm-6">
                <div class="table-responsive">
                    <table class="table table-bordered"
                    width='20%'cellspacing="0">
                        <thead>
                            <tr>
                                <th>No</th>
                                <th width='60%'>Gambar</th>
                                <th>Aksi</th>
                            </tr>
                        </thead>

                        <tbody>
                        <?php
                            // include database
                            include 'database.php';
                            // perintah sql untuk
                            menampilkan daftar bank yang
                            berelasi dengan tabel kategori
                            bank
                            $sql="select * from gambar order
                            by id_gambar desc";
                            $hasil=mysqli_query($kon,$sql);
                            $no=0;
                            //Menampilkan data dengan
                            perulangan while
                            while ($data = mysqli_fetch_
                            array
                            ($hasil)):
                            $no++;
                        ?>
                        <tr>
                            <td><?php echo $no; ?></td>
                            <td><img src="gambar/<?php
                            echo $data['gambar'];?>"
                            class="rounded" width='100%'
                            alt="Cinque Terre"></td>
                            <td><a
                            href="hapus.php?id_gambar=
                        <?php echo $data['id_gambar'];?>
                        &gambar=<?php echo $data['gambar'];
                        ?
                        >
                        " onclick="konfirmasi()" class="btn
                        btn-danger" role="button">Hapus</a>
                        </td>
                        </tr>
                        <!-- bagian akhir (penutup) while
                        -->
                        <?php endwhile; ?>

                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

Source Code : simpan.php < Untuk menyimpan file hasil upload

<?php

    if (isset($_POST['btn_simpan'])) {
        //Include file koneksi, untuk koneksikan ke database
        include 'database.php';
        //Cek apakah ada kiriman form dari method post
        if ($_SERVER["REQUEST_METHOD"] == "POST") {

            $ekstensi_diperbolehkan = array('png','jpg');
            $gambar = $_FILES['gambar']['name'];
            $x = explode('.', $gambar);
            $ekstensi = strtolower(end($x));
            $file_tmp = $_FILES['gambar']['tmp_name'];

            if (!empty($gambar)){
                if (in_array($ekstensi,
                    $ekstensi_diperbolehkan) === true){
   
                    //Mengupload gambar
                    move_uploaded_file($file_tmp, 'gambar/'.
                    $gambar);

                    $sql="insert into gambar (gambar) values
                     ('$gambar')";

                    $simpan_bank=mysqli_query($kon,$sql);

                    if ($simpan_bank) {
                        header("Location:index.php?add=
                    berhasil");
                    }
                    else {
                        header("Location:index.php?add=
                    gagal");
                    }
                   
                }
            }else {
                $gambar="bank_default.png";
            }
        }

    }
?>

Source Code : hapus.php <Untuk menghapus baris yang ada di table

<?php
    include 'database.php';


    $id_gambar=$_GET["id_gambar"];
    $gambar=$_GET["gambar"];
    $sql="delete from gambar where id_gambar=$id_gambar";
    $hapus_bank=mysqli_query($kon,$sql);


    //Menghapus file gambar
    unlink("gambar/".$gambar);


    if ($hapus_bank) {
        header("Location:index.php?hapus=berhasil");
    }
    else {
        header("Location:index.php?hapus=gagal");


    }
?>

Hasil




Posting Komentar

Lebih baru Lebih lama