0

I made a form where I upload multiple photos and then stored them on a single row in my database. How can I echo all the photos? Below is my sample code trying to display the photos from database and screenshot of my database and how it look like when I run the code.

<?php include($_SERVER['DOCUMENT_ROOT'].'/project/admin/add/commsheetConnect.php'); ?>
<style type="text/css">
    .comms-image img{
        display: block;
    }
</style>
<?php   
    $conn = mysqli_connect(db_host, db_username, db_password, db_name, db_port);
    $sql = "SELECT image FROM comms_images";
    $res = mysqli_query($conn, $sql);
    while ($row = mysqli_fetch_array($res)) {
        echo "<div class='comms_image'>";
        //DISPLAY IMAGE
        echo "<img src='/project/decors/photos/comms_img/".$row['image']."'>";
    }
?>

What the photos look like in my database: image

What the code looks like when I run it:

second image

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 2
    Do not store the data like that. Store one photo per row. – user3783243 Feb 12 '21 at 18:27
  • 1
    You could use `explode` or https://www.php.net/manual/en/function.str-getcsv.php but you really should fix the schema issue now before it becomes a large issue. (e.g. a file has a comma in its name, you need to find one row in your table that has a particular name, etc) – user3783243 Feb 12 '21 at 18:31
  • You should save one image per row just like @user3783243 said. – Fresz Feb 12 '21 at 18:32
  • 1
    Don't bother with this design. Remove that column from the database and instead create a dedicated table for images. Never store comma-separated values in the database. – Dharman Feb 12 '21 at 18:37
  • I recommend do not store the image inside the table. I upload the file into the server with copy(). I just save in the table the original name and the name I use to save the image. – Zeke Feb 12 '21 at 18:39
  • 1
    Oh so storing files separated with comma is bad? I'm sorry I'm a newbie in MySQL. – helvetica moon Feb 12 '21 at 18:46
  • 1
    thank you all so much! ill fix now my database – helvetica moon Feb 12 '21 at 18:48

0 Answers0