'why the picture gone after do the update data in PHP

lets say i have 2 column in my inventory data called category and image_name in my php and database.

image_name are coming from this (basically in my database, image_name contain the link of the uploaded image) :

<?php


                    if ($_SERVER["REQUEST_METHOD"] == "POST") {
                      $file = $_FILES['uploadgambar'];

                      $fileName = $_FILES['uploadgambar']['name'];
                      $fileTmpName = $_FILES['uploadgambar']['tmp_name'];
                      $fileSize = $_FILES['uploadgambar']['size'];
                      $fileError = $_FILES['uploadgambar']['error'];
                      $fileType = $_FILES['uploadgambar']['type'];


                      $fileExt = explode('.', $fileName);
                      $fileActualExt = strtolower(end($fileExt));

                      $allowed = array('jpg', 'jpeg', 'png');

                      if (in_array($fileActualExt, $allowed)) {
                        if ($fileError === 0) {
                          if ($fileSize < 5000000) {
                            $fileNameNew = uniqid('', true) . "." . $fileActualExt;
                            $fileDestination = 'uploads/' . $fileNameNew;
                            move_uploaded_file($fileTmpName, $fileDestination);
                            //  header("Location: index.php?uploadsuccess");
                          } else {
                            echo "File anda terlalu besar (maximal 1gb)";
                          }
                        } else {
                          echo "Terdapat error dalam mengupload file";
                        }
                      } else {
                        echo "Anda tidak bisa upload file ini karena tidak berbentuk JPG/JPEG/PNG";
                      } 

i build some code for update feature for category and image_name column like this

$kategori = mysqli_real_escape_string($conn, $_POST["kategori"]);
$image_name = mysqli_real_escape_string($conn, $fileDestination);

and here's for the update

   $sql1 = "update $tabeldatabase set kategori='$kategori', image_name = '$image_name' where kode = '$kode';

$updatean = mysqli_query($conn, $sql1);
                          echo "<script type='text/javascript'>  alert('Berhasil, Data barang telah diupdate!'); </script>";
                          echo "<script type='text/javascript'>window.location = '$forwardpage';</script>";

but the problem is, when i update the image its run, but when i update kategori, idk why my picture gone and when i look at the database, column image_name has removed for that id and cause the image gone.

my expected result is, just like update feature, if i update the category, then the image_name wont missing.



Solution 1:[1]

I am writing your answer, but maybe it needs some editions. so add some comments if the ways not fix the problem.

one of the reasons maybe due to updating the row of database even if the file name is empty. so you must check the file name before update the database table record.

In this code, whenever you don't have upload file, so $fileDestination will be empty, so the cell of table will be empty and image address will gone!

For that problem, you could change the query:

$sql1 = "update $tabeldatabase set kategori='$kategori', image_name = '$image_name' where kode = '$kode';

to something like this:

if ($image_name) {
  $sql1 = "update $tabeldatabase set kategori='$kategori', image_name = '$image_name' where kode = '$kode';
} else {
  $sql1 = "update $tabeldatabase set kategori='$kategori' where kode = '$kode';
}

Apart from this I recommend you to change this row:

$sql1 = "update $tabeldatabase set kategori='$kategori', image_name = '$image_name' where kode = '$kode';

to a fixed table name like this:

$sql1 = "update my_static_table_name set kategori='$kategori', image_name = '$image_name' where kode = '$kode';

so you can easily debug the problem.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1