-2

How can I show multiple images from database? I already tried but the images don't show up.

Database

database

Output

page

Code

<div class="form-group">
        <label for="gambar">Foto KTP</label>
        <input type="file" class="form-control-file border" id="ktp1" name="ktp" multiple>
      </div><br>
      <button type="submit" class="btn btn-primary" name="tambah">Proses</button>
      <button type="reset" class="btn btn-danger" name="reset">Hapus</button> 
    <?php 
    
      if(isset($_POST['tambah'])){
        $id_rekening = $_POST['id_rekening'];
        $nama_file = $_FILES['ktp']['name'];
        $foto = $_FILES['ktp']['name'];
        $source = $_FILES['ktp']['tmp_name'];
        $folder = './assets/';

        move_uploaded_file($source, $folder.$nama_file);
        move_uploaded_file($source, $folder.$foto);
        $insert = mysqli_query($koneksi, "INSERT INTO pelanggan VALUES (NULL, '$id_rekening', '$nama_file, $foto')");
  • Why are you storing the same file name twice? – El_Vanja Dec 25 '20 at 17:23
  • **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/5741187) – Dharman Dec 25 '20 at 23:01

2 Answers2

0

I haven't looked at your image upload code -- I assume that you have checked the images are in the correct place on the server and that the file names in the database match the image files.

Problem

Your code doesn't work because you set your images with attributes like:

src='assets/daging2.jpeg, daging2.jpeg'

When you should be outputting two separate image tags.

<img src='assets/daging2.jpeg' ... >
<img src='assets/daging2.jpeg' ... >

Additionally you should be using prepared statements for any user generated input.

Updated code

$nomor = 1;
$id    = $_GET["id"] ?? NULL;

// Check something was submitted
if( $id ){
    $dapat = $koneksi->prepare("
        SELECT *
        FROM pemesanan
            JOIN pelanggan ON pemesanan.id_pemesanan = pelanggan.id_pemesanan
        WHERE pemesanan.id_pemesanan = ?
    ");
    $dapat->bind_param("i", $id); // Assuming `id` is an integer
    $dapat->execute();
    
    $result = $dapat->get_result();
    
    while ( $data = $result->fetch_assoc() ){
        echo "<tr><td>{$data['id_rekening']}</td>";
    
        // Split the string into file names
        $image_array = explode(", ", $data["ktp"]);

        // Output the images
        foreach($image_array as $image){
            echo "<td><img src='assets/{$image}' width='600' height='450'/></td>";
        }
    
        echo "</tr>";
    }
}
Steven
  • 6,053
  • 2
  • 16
  • 28
0

Based on this what I see you have the $data['ktp'] array with the image name and extension. The reason why you do not see image is that in the img src is 2 times the image and nothing responds to it. You can try using this for your image:

$images_arr = explode(",",$data['ktp']);

foreach($images_arr as $img){
echo '<td><img src="assets/'.$img.'" width="600" height="450" alt=""></td>";
}

This will loop the whole array and display the images.

mrSotirow
  • 103
  • 1
  • 4