Minggu, 05 November 2017

MEMBUAT PERINTAH UPDATE PADA PHP MYSQL

MEMBUAT PERINTAH UPDATE (CRUD)


Berikut ini adalah tampilan sebelumnya di halaman Home



langkah-langkah proses update sebagai berikut :


1. Tuliskan source code
 nya, dan buka file nya di folder (..Praktikum/admin/pages), dan buat script seperti dibawah ini:
   

<div class="col-md-6 col-md-offset-3">
<?php
$sql_edit ="SELECT * FROM berita WHERE id_berita=".$_GET['id'];
$r = $conn->query($sql_edit)->fetch_array();
//eksekusi edit
if(isset($_POST['edit'])){
                //variabel yang diedit
                $gambar = $_FILES["gambar"]["name"];
                $judul = $_POST['judul'];
                $isi = htmlentities($_POST['isi']);
                //pengecekan dan unggah gambar
if(!empty($_FILES["gambar"]["name"])){
                $target_dir = "gambar/";
                $target_file = $target_dir . basename($_FILES["gambar"]["name"]);
                $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
                $check = getimagesize($_FILES["gambar"]["tmp_name"]);
                if($check !== false){
                                if(move_uploaded_file($_FILES["gambar"]["tmp_name"], $target_file)){
                                                $sql = "UPDATE berita SET judul='".$judul."', isi ='".$isi."', gambar ='".$gambar."' WHERE id_berita ='".$_GET['id']."'";
                                                $ok = 1;
}else{
                                                echo '<div class="alert alert-danger">';
                                                echo '<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>';
                                                echo "Maaf Terjadi Kesalahan Saat Mengunggah Gambar";
                                                echo '</div>';
                                                $ok = 0;
                }
}else{
                echo '<div class="alert alert-danger">';
                echo '<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>';
                echo "File Yang Diunggah Bukan Gambar";
                echo '</div>';
                $ok = 0;
                }
}else{
                $sql = "UPDATE berita SET judul='".$judul."', isi ='".$isi."', gambar ='".$gambar."' WHERE id_berita ='".$_GET['id']."'";
                $ok = 1;               }
if($ok ==1){
                $result = $conn->query($sql);
                if($result === TRUE) {
                                echo '<div class="alert alert-success">';
                                echo '<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>';
                                echo "Berita Telah Berhasil Di Ubah";
                                echo '</div>';
                                header('Location: index.php');
}else{
                echo '<div class="alert alert-danger">';
                                echo '<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>';
                                echo "Error: " .$sql . "<br>" . $conn->error;
                                echo '</div>';
                                }
                }
}
?>
</div>
<div class="row">
                <div class="col-md-6 col-md-offset-3 thumbnail">
                                <h4 class="text-center">Ubah Berita</h4>
                                <form method="post" enctype="multipart/form-data">
                                                <label>Gambar Berita :</label>
                                                <input type="file" name="gambar" class="form-control"><hr>
                                                <label>Judul Berita :</label>
                                                <input type="text" name="judul" class="form-control" required value="<?php echo $r['judul'] ?>"><hr>
                                                <label>Isi Berita :</label>
                                                <textarea name="isi" class="form-control" required><?php echo $r['isi'] ?></textarea><hr>
                                                <input type="submit" name="edit" class="btn btn-success" value="Simpan">
                                </form>
                </div>
</div>


2. selanjutnya klik icon Ubah pada halaman Home yang tersedia seperti gambar di bawah ini


3. lalu kita masuk ke halaman ubah 



4.  Setelah selesai melakukan pengubahan isi berita jangan lupa di klik button Simpannya, dan hasilnya akan berubah..



SELEASAI






Tidak ada komentar:

Posting Komentar