0

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");
    }
}
tham
  • 11
  • 1
  • Have you tried printing $kategori in the konf_edit_ktgr.php file? What does it say? – Viktor May 23 '22 at 20:12
  • it say Undefined array key "kategori" @Viktor – tham May 23 '22 at 20:16
  • What does `var_dump($_POST)` show? – Barmar May 23 '22 at 20:19
  • Warning: Undefined array key "kategori" in C:\xampp\htdocs\admin-smk\admin\konf_edit_ktgr.php on line 6 array(0) { } @Barmar – tham May 23 '22 at 20:21
  • In addition to your current issue you are open to SQL injections. Queries should be parameterized and put in prepared statements. – user3783243 May 23 '22 at 20:22
  • The form looks fine to me, I don't see why that's empty. – Barmar May 23 '22 at 20:23
  • when I click "edit" from the table I can get the data and it show on a input field, and then I edit it for example from "alex" to "alexa". But when I click the submit button it always get alert "the data must be filled in" and it back to "alex" but still on that input field, not the table. And also the database did'nt change @Barmar – tham May 23 '22 at 20:29
  • Do you have any JavaScript changing the way the form is submitted? – Barmar May 23 '22 at 20:33
  • I didn't change anything except those php things @Barmar – tham May 23 '22 at 20:59
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman May 24 '22 at 09:29

1 Answers1

1

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>
SlametS24
  • 26
  • 1