kok fungsi update saya gagal terus yah...


<?php
    //Koneksi Database
    $server = "localhost";
    $user = "root";
    $pass = "";
    $database = "aplikasipanjar";

    $koneksi = mysqli_connect($server, $user, $pass, $database)or die(mysqli_error($koneksi));


//jika tombol simpan diklik

if(isset($_POST['bsimpan']))

{
    //Pengujian Apakah data akan diedit atau disimpan baru
    if($_GET['hal'] == "edit")
    {
        //Data akan di edit
        $edit = mysqli_query($koneksi, "UPDATE biayagugatan SET
                                             id_rad = '$_POST[trad]',
                                             radius = '$_POST[tradius]',

                                         WHERE id_no = '$_GET[id]'
                                       ");
        if($edit) //jika edit sukses
        {

            echo "<script>
                    alert('Edit data suksess!');
                    document.location='index.php';
                 </script>";
        }
        else
        {
            echo "<script>
                    alert('Edit data GAGAL!!');
                    document.location='index.php';
                 </script>";
        }
    }
    else
    {
        //Data akan disimpan Baru
        $simpan = mysqli_query($koneksi, "INSERT INTO biayagugatan (id_rad, radius)
                                      VALUES ('$_POST[trad]',
                                               '$_POST[tradius]')
                                     ");
        if($simpan) //jika simpan sukses
        {
            echo "<script>
                    alert('Simpan data suksess!');
                    document.location='index.php';
                 </script>";
        }
        else
        {
            echo "<script>
                    alert('Simpan data GAGAL!!');
                    document.location='index.php';
                 </script>";
        }
    }


}

    //Pengujian jika tombol Edit / Hapus di klik
    if(isset($_GET['hal']))
    {
        //Pengujian jika edit Data
        if($_GET['hal'] == "edit")
        {
            //Tampilkan Data yang akan diedit
            $tampil = mysqli_query($koneksi, "SELECT * FROM biayagugatan WHERE id_no = '$_GET[id]' ");
            $data = mysqli_fetch_array($tampil);
            if($data)
            {
                //Jika data ditemukan, maka data ditampung ke dalam variabel
                $vrad = $data['id_rad'];
                $vradius = $data['radius'];
            }
        }
        else if ($_GET['hal'] == "hapus")
        {
            //Persiapan hapus data
            $hapus = mysqli_query($koneksi, "DELETE FROM biayagugatan WHERE id_no = '$_GET[id]' ");
            if($hapus){
                echo "<script>
                        alert('Hapus Data Suksess!!');
                        document.location='index.php';
                     </script>";
            }
        }
    }

?>

<!DOCTYPE html>
<html>
<head>
    <title>CRUD 2020 PHP & MySQL + Bootstrap 4</title>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
</head>
<body>
<div class="container">

    <h1 class="text-center">Halaman Radius</h1>
    <h2 class="text-center">Test</h2>

    <!-- Awal Card Form -->
    <div class="card mt-3">
      <div class="card-header bg-primary text-white">
        Form Input
      </div>
      <div class="card-body">
        <form method="post" action="">
            <div class="form-group">
                <label>Radius</label>
                <input type="text" name="trad" value="<?=@$vrad?>" class="form-control" placeholder="Input disini!" required>
            </div>
            <div class="form-group">
                <label>Biaya Radius</label>
                <input type="text" name="tradius" value="<?=@$vradius?>" class="form-control" placeholder="Input disini!" required>
            </div>
            <button type="submit" class="btn btn-success" name="bsimpan">Simpan</button>
            <button type="reset" class="btn btn-danger" name="breset">Kosongkan</button>
        </form>
      </div>
    </div>
    <!-- Akhir Card Form -->

    <!-- Awal Card Tabel -->
    <div class="card mt-3">
      <div class="card-header bg-success text-white">
        Daftar Mahasiswa
      </div>
      <div class="card-body">

        <table class="table table-bordered table-striped">
            <tr>
                <th>No.</th>
                <th>Radius</th>
                <th>Biaya Radius</th>
                <th>Aksi</th>
            </tr>
            <?php
                $no = 1;
                $tampil = mysqli_query($koneksi, "SELECT * from biayagugatan order by id_no desc");
                while($data = mysqli_fetch_array($tampil)) :

            ?>
            <tr>
                <td><?=$no++;?></td>
                <td><?=$data['id_rad']?></td>
                <td><?=$data['radius']?></td>
                <td>
                    <a href="index.php?hal=edit&id=<?=$data['id_no']?>" class="btn btn-warning"> Edit </a>
                    <a href="index.php?hal=hapus&id=<?=$data['id_no']?>"
                       onclick="return confirm('Apakah yakin ingin menghapus data ini?')" class="btn btn-danger"> Hapus </a>
                </td>
            </tr>
        <?php endwhile; //penutup perulangan while ?>
        </table>

      </div>
    </div>
    <!-- Akhir Card Tabel -->

</div>

<script type="text/javascript" src="js/bootstrap.min.js"></script>
</body>
</html>
avatar ganemchannel
@ganemchannel

1 Kontribusi 0 Poin

Diperbarui 3 tahun yang lalu

Tanggapan

Baca aturan main di forum ini ya. Biasakan bikin judul yang relevan. contoh "kode update PHP gagal". 2. Tuliskan penjelasan di pertanyaan kamu, jangan hanya copy paste kodenya

1 Jawaban:

<div>Ubah bagian :</div><pre>$edit = mysqli_query($koneksi, "UPDATE biayagugatan SET id_rad = '$_POST[trad]', radius = '$_POST[tradius]',

                                     WHERE id_no = &#039;$_GET[id]&#039;
                                   &quot;);&lt;/pre&gt;&lt;div&gt;Menjadi :&lt;/div&gt;&lt;pre&gt;$edit = mysqli_query($koneksi, &quot;UPDATE biayagugatan SET
                                         id_rad = &#039;$_POST[trad]&#039;,
                                         radius = &#039;$_POST[tradius]&#039;
                                         
                                     WHERE id_no = &#039;$_GET[id]&#039;
                                   &quot;);&lt;/pre&gt;&lt;div&gt;&lt;br&gt;Note : Kelebihan koma itu, lain kali teliti ya 😁&lt;br&gt;&lt;br&gt;Semoga membantu&lt;/div&gt;
avatar syahid246
@syahid246

70 Kontribusi 160 Poin

Dipost 3 tahun yang lalu

Login untuk ikut Jawaban