-1

Image filename from database should appear here

Image filename from database should appear here

I insert a image from database to update form but it become like this

HTML FORM:

<form method="post" action="functionUpdate.php?id=<?php echo $id; ?>" enctype="multipart/form-data">
  <div class="main-container">
      <div class="gambar-pelajar">
        <label for="studentImage">Gambar Pelajar</label>
        <input type="file" name="studentImage" value="<?=$studentImage?>" id="studentImage">
      </div>
    </div>
</form>

Retrieve Image from DB:

<?php
include "config.php";
$id=$_GET['id'];
// AUTO FILL FIELD 
$query = "SELECT * FROM student WHERE id='$id'";
$rslt = mysqli_query($conn,$query);
while ($row = mysqli_fetch_array($rslt)) {
    $studentImage = $row['studentImage'];
}
?>
Yousaf
  • 27,861
  • 6
  • 44
  • 69
  • You can't populate a `file`-input. It just contains a reference to a file on the client, which the client then reads and uploads on submit. It doesn't contain the actual file-data. – M. Eriksson Sep 17 '20 at 10:36
  • To view the selected file you need a preview element, like – Tasos Sep 17 '20 at 16:11
  • **Warning:** You are wide open to [SQL Injections](https://stackoverflow.com/a/60496/1839439) 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/5741187) – Dharman Oct 01 '20 at 10:59

2 Answers2

0

Update your code mysqli_fetch_array to mysqli_fetch_assoc

<?php
include "config.php";
$id=$_GET['id'];
// AUTO FILL FIELD 
$query = "SELECT * FROM student WHERE id='$id'";
$rslt = mysqli_query($conn,$query);
while ($row = mysqli_fetch_assoc($rslt)) {
    $studentImage = $row['studentImage'];
}
?>
  • How would this solve the OP's issue? Using `fetch_array()` still works since that returns an array with the data both as indexed and associative. I agree that it would be better to use `fetch_assoc()` but that's not what the question is about. If you look at the image they've posted, they do get the data. The issue is that they are trying to populate the file-input with the file contents. – M. Eriksson Sep 17 '20 at 11:28
0

You need to show the file using img tag not a input tag

 <img src="<?= $studentImage?> " id="studentImage" name="studentImage">