tham
tham

Reputation: 11

Can't update mysql php

I'm trying to update data from mysql database use php. I can get all data from db into table, then I click edit and I can get the data, but when I edit the form and click the submit button it's always said that the field is empty. Or sometimes it's edited into "blank row" on mysql db

heres the code: ktgr_soal.php

<?php

include('../koneksi/koneksi.php');
if ((isset($_GET['aksi'])) && (isset($_GET['data']))) {
  if ($_GET['aksi'] == 'hapus') {
    $kode_ktgr = $_GET['data'];

    //hapus data
    $sql_ktgr = "delete from `kategori_soal` 
  where `id_kategori` = '$kode_ktgr'";
    mysqli_query($koneksi, $sql_ktgr);
  }
}
?>

<head>
  <?php include("includes/head.php") ?>
</head>

<body>
  <!-- S I D E B A R -->
  <?php include("includes/sidebar.php") ?>
  <!--  ENDS I D E B A R -->

  <?php include("includes/header.php") ?>


  <div class="content-wrap">
    <div class="main">
      <div class="container-fluid">
        <div class="row">
          <div class="col-lg-8 p-r-0 title-margin-right">
            <div class="page-header">
              <div class="page-title">
                <h1>Data Kategori Soal</h1>
              </div>
            </div>
          </div>
          <!-- /# column -->
          <div class="col-lg-4 p-l-0 title-margin-left">
            <div class="page-header">
              <div class="page-title">
                <ol class="breadcrumb">
                  <li class="breadcrumb-item"><a href="#">Dashboard</a></li>
                  <li class="breadcrumb-item active">Kategori Soal</li>
                </ol>
              </div>
            </div>
          </div>
          <!-- /# column -->
        </div>
        <!-- /# row -->

        <!-- M A I N  C O N T E N T -->
        <section id="main-content">
          <div class="row">
            <div class="col-lg-12">
              <div class="card">
                <div class="card-title">
                  <a href="add_kategori.php">
                    <button type="button" class="btn btn-primary btn-flat btn-addon m-b-10 m-l-5 float-right"><i class="ti-plus"></i>Tambah Data</button>
                  </a>
                  <!-- Search form -->
                  <form class="form-inline d-flex md-form form-sm">
                    <input class="form-control form-control-sm mr-3 w-25" type="text" placeholder="Search" aria-label="Search">
                    <i class="ti-search" aria-hidden="true"></i>
                  </form>


                </div>
                <div class="card-body">
                  <div class="col-sm-12">
                    <?php if (!empty($_GET['notif'])) { ?>
                      <?php if ($_GET['notif'] == "tambahberhasil") { ?>
                        <div class="alert alert-success" role="alert">
                          Data Berhasil Ditambahkan</div>
                      <?php } else if ($_GET['notif'] == "editberhasil") { ?>
                        <div class="alert alert-success" role="alert">
                          Data Berhasil Diubah</div>
                      <?php } ?>
                    <?php } ?>
                  </div>

                  <div class="table-responsive">
                    <table class="table">
                      <thead>
                        <tr>
                          <th>No</th>
                          <th>Kategori Soal</th>
                          <th></th>
                        </tr>
                      </thead>
                      <tbody>
                        <?php
                        $batas = 10;
                        if (!isset($_GET['halaman'])) {
                          $posisi = 0;
                          $halaman = 1;
                        } else {
                          $halaman = $_GET['halaman'];
                          $posisi = ($halaman - 1) * $batas;
                        }
                        ?>
                        <?php include('../koneksi/koneksi.php') ?>
                        <?php

                        //menampilkan data hobi
                        $sql_ktgr = "SELECT * FROM `kategori_soal` ";
                        if (isset($_GET["katakunci"])) {
                          $katakunci_jurusan = $_GET["katakunci"];
                          $sql_ktgr .= " where `kategori` LIKE '%$katakunci_jurusan%'";
                        }
                        $sql_ktgr .= " order by `kategori` limit $posisi, $batas ";

                        $query_ktgr = mysqli_query($koneksi, $sql_ktgr);
                        $no = $posisi + 1;
                        while ($data_ktgr = mysqli_fetch_row($query_ktgr)) {
                          $kode_ktgr = $data_ktgr[0];
                          $kategori = $data_ktgr[1];

                        ?>

                          <tr>
                            <th scope="row"><?php echo $no; ?></th>
                            <td><?php echo $kategori; ?></td>
                            <td>
                              <a href="edit_kategori.php?data=<?php echo $kode_ktgr; ?>">
                                <button type="button" class="btn btn-warning btn-sm m-b-10 m-l-5 "><i class="ti-pencil-alt"></i></button>
                              </a>
                              <a href="javascript:if(confirm('Anda yakin ingin menghapus data <?php echo $kategori; ?>?'))
                            window.location.href = 'ktgr_soal.php?aksi=hapus&data=<?php echo $kode_ktgr; ?>'" class="btn btn-danger btn-sm m-b-10 m-l-5"><i class="ti-trash"></i>
                              </a>
                            </td>
                          </tr>
                        <?php
                          $no++;
                        } ?>

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

            <!-- /# column -->


            <!-- FOOTER -->
            <?php include("includes/footer.php") ?>
        </section>
      </div>
    </div>
  </div>

  <!-- script -->
  <?php include("includes/script.php") ?>
</body>

</html>

edit_kategori.php

<?php
session_start();
include('../koneksi/koneksi.php');
if (isset($_GET['data'])) {
  $kode_kat = $_GET['data'];
  $_SESSION['kode_ktgr'] = $kode_kat;

  //get data kategori soal
  $sql_d = "select `kategori` from `kategori_soal` where `id_kategori` = '$kode_kat'";
  $query_d = mysqli_query($koneksi, $sql_d);
  while ($data_d = mysqli_fetch_row($query_d)) {
    $kategori = $data_d[0];
  }
}
?>

<head>
  <?php include("includes/head.php") ?>
</head>

<body>
  <!-- S I D E B A R -->
  <?php include("includes/sidebar.php") ?>
  <!--  ENDS I D E B A R -->

  <?php include("includes/header.php") ?>


  <div class="content-wrap">
    <div class="main">
      <div class="container-fluid">
        <div class="row">
          <div class="col-lg-8 p-r-0 title-margin-right">
            <div class="page-header">
              <div class="page-title">
                <h1>Edit Kategori Soal</h1>
              </div>
            </div>
          </div>
          <!-- /# column -->
          <div class="col-lg-4 p-l-0 title-margin-left">
            <div class="page-header">
              <div class="page-title">
                <ol class="breadcrumb">
                  <li class="breadcrumb-item"><a href="#">Dashboard</a></li>
                  <li class="breadcrumb-item active">Home</li>
                </ol>
              </div>
            </div>
          </div>
          <!-- /# column -->
        </div>
        <!-- /# row -->

        <!-- M A I N  C O N T E N T -->
        <section id="main-content">
          <div class="row">
            <div class="col-lg-12">
              <div class="card">
                <div class="card-title">
                  <h3>Edit Kategori Soal </h3>
                </div>
                <div class="card-body">
                  <div class="basic-form">

                    <?php
                    if (!empty($_GET['notif'])) {
                      if ($_GET['notif'] == "editkosong") {
                    ?>
                        <div class="alert alert-danger" role="alert">Maaf data wajib di isi</div>
                    <?php
                      }
                    }
                    ?>


                    <form action="konf_edit_ktgr.php" action="post">
                      <div class="form-group">
                        <label for="kategori">Kategori</label>
                        <input type="text" id="kategori" name="kategori" class="form-control input-focus" value="<?php echo $kategori; ?>">
                      </div>
                      <button type="submit" class="btn btn-primary">Submit</button>
                    </form>

                  </div>
                </div>
              </div>
            </div>
          </div>

          <!-- /# column -->


          <!-- FOOTER -->
          <?php include("includes/footer.php") ?>
        </section>
      </div>
    </div>
  </div>

  <!-- script -->
  <?php include("includes/script.php") ?>
</body>

</html>

konf_edit_ktgr.php

<?php
session_start();
include('../koneksi/koneksi.php');
if (isset($_SESSION['kode_ktgr'])) {
    $kode_kat = $_SESSION['kode_ktgr'];
    $kategori = $_POST['kategori'];


    if (empty($kategori)) {
        header("Location:edit_kategori.php?data=" . $kode_kat . "&notif=editkosong");
    } else {
        $sql = "update `kategori_soal` set `kategori` = '$kategori' where `id_kategori` = '$kode_kat'";
        mysqli_query($koneksi, $sql);
        header("Location:ktgr_soal.php?notif=editberhasil");
    }
}

Upvotes: 0

Views: 92

Answers (1)

SlametS24
SlametS24

Reputation: 26

try to check again "$kode_kat" which is in the file "konf_edit_ktgr.php" whether you have got the value or not. If not, insert "$kode_kat" in the Edit form like this:

<form action="konf_edit_ktgr.php" action="post">
 <div class="form-group">
 <label for="kategori">Kategori</label>
  <input type="text" id="kategori" name="kategori" class="form-control input-focus" value="<?php echo $kategori; ?>">
   </div>
   <input type="hidden" name="id" value="<?php echo $kode_kat; ?>">
 <button type="submit" class="btn btn-primary">Submit</button>
 </form>

Upvotes: 1

Related Questions