0

I'm having a small college project about discussion room service. I'm stuck at updating the database of the rooms. I already used mysqli_error() function, and that didn't return any error, I wonder why. Here's my form code:

<?php
//Tahap 1. Buat koneksi Database
$host = "localhost";
$user = "root";
$pass = "";
$name = "pinjamruang";
$koneksi = mysqli_connect($host, $user, $pass, $name);

//Periksa apakah koneksi berhasil
if(mysqli_connect_errno()){
    echo "Error: ";
    echo mysqli_connect_error();
    echo "<br   /> Error Code: ";
    echo mysqli_connect_errno();
    die();
}
$sql = "SELECT * FROM ruangan";
$keranjang = mysqli_query($koneksi, $sql);
$row = mysqli_fetch_assoc($keranjang);
?>

<h1 class="page-header">Edit Karyawan</h1><br>

<form class="form-horizontal" action="process/process-ruangan-edit.php" method="post" enctype="multipart/form-data">
    <div class="form-group">        
        <label for="inputKodeRuangan" class="col-sm-2 control-label">Kode Ruangan</label>
        <div class="col-sm-10">
            <input type="text" name="kode" class="form-control" id="inputKodeRuangan" value="<?php echo $row['kode'];?>" placeholder="Kode Ruangan">
        </div>
        </div>
        <div class="form-group">
        <label for="inputJumlahMeja" class="col-sm-2 control-label">Jumlah Meja</label>
        <div class="col-sm-10">
            <input type="number" name="meja" class="form-control" id="inputJumlahMeja" value="<?php echo $row['meja'];?>"placeholder="Jumlah Meja">
        </div>
        </div>
        <div class="form-group">
        <label for="inputJumlahKursi" class="col-sm-2 control-label">Jumlah Kursi</label>
        <div class="col-sm-10">
            <input type="number" name="kursi" class="form-control" id="inputJumlahKursi" value="<?php echo $row['kursi'];?>"placeholder="Jumlah Kursi">
        </div>
        </div>
        <div class="form-group">
            <label for="inputStatus" class="col-sm-2 control-label">Status</label>
            <div class="col-sm-10">
                <select name="status" class="form-control" id="inputStatus">
                    <option value="available">Tersedia</option>
                    <option value="unavailable">Tidak Tersedia</option>
                </select>
            </div>
        </div>
        <div class="form-group">
        <label for="inputNote" class="col-sm-2 control-label">Catatan Khusus</label>
        <div class="col-sm-10">
            <input type="text" name="note" class="form-control" id="inputNote" value="<?php echo $row['note'];?>"placeholder="Catatan Khusus">
        </div>
        </div>      
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <input type="hidden" name="id" value="<?php echo $row2['id']; ?>" />
            <button type="submit" class="btn btn-primary">Update</button>
        </div>
    </div>
</form>

And here's my process code:

<?php
// Tahap 1. Buat koneksi database
$host = "localhost";
$user = "root";
$pass = "";
$name = "pinjamruang";
$koneksi = mysqli_connect($host, $user, $pass, $name);

//Periksa apakah koneksi berhasil
if(mysqli_connect_errno()){
    echo "Error: ";
    echo mysqli_connect_error();
    echo "<br   />Error Code: ";
    echo mysqli_connect_errno();
    die();
}
//Tahap 2. Lakukan Query SQL
// Dapatkan data dari form dan dibersihkan
$kode = mysqli_real_escape_string($koneksi, $_POST['kode']);
$meja = mysqli_real_escape_string($koneksi, $_POST['meja']);
$kursi = mysqli_real_escape_string($koneksi, $_POST['kursi']);
$status = mysqli_real_escape_string($koneksi, $_POST['status']);
$note = mysqli_real_escape_string($koneksi, $_POST['note']);
$sql = "UPDATE ruangan
        SET kode = '$kode',
            kursi = $kursi,
            meja = $meja,
            status = '$status',
            note = '$note'
        WHERE id = $_POST[id]";
mysqli_query($koneksi,$sql);
echo mysqli_error($koneksi);
//header('Location: ../index.php?page=ruangan');
?>

Any help would be much appreciated, I'm still really new at PHP and basically programming so, thanks a lot!

  • 1
    You should echo your `mysqli_error(..)` to know what the error is. – Karlo Kokkak May 13 '18 at 14:16
  • And you only need the one AFTER you run the `mysqli_query()` – RiggsFolly May 13 '18 at 14:28
  • Your script is wide open to [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) in either the `MYSQLI_` or `PDO` API's – RiggsFolly May 13 '18 at 14:29
  • Also you dont check any of the `$_POST` variable actually exist before attempting to use them in the query – RiggsFolly May 13 '18 at 14:30
  • Alright, my bad. Thanks for the reminder, @KarloKokkak. Now that i echoed the error, it returns this: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' Notice: Undefined variable: row2 in D:\Kuliah\PHP\htdocs\proj' at line 7 – Vanan Andreas May 13 '18 at 14:35
  • 1
    In your first code block - change - `$row2['id']` to - `$row['id']` . You're passing an `id` with no value. – Karlo Kokkak May 13 '18 at 14:37
  • Even though my code at line 7 doesn't have any variable called row 2.. – Vanan Andreas May 13 '18 at 14:38
  • The FIRST script does see `` – RiggsFolly May 13 '18 at 14:38
  • Thanks everyone! Hope you guys have a great day! – Vanan Andreas May 13 '18 at 15:09

1 Answers1

1

In your form code you are referencing $row2 which hasn't been defined yet.

<input type="hidden" name="id" value="<?php echo $row2['id']; ?>" />

You should change it to

<input type="hidden" name="id" value="<?php echo $row['id']; ?>" />
Rubixryan
  • 107
  • 7