Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

342
Vistas
How can I correctly display an image from my database/table using a php form?

Here are the following scripts I have created. first is the form that allows to select an image to be displayed. Second is the display page. My question is how can I display images from my form using a database/table in php?? I only want to display a single image that I have selected using the select file form.

<!DOCTYPE html>
<html>
<body>

 <form action="display.php" method="post" enctype="multipart/form-data">
    Select image to display:
    <input type="file" name="fileToDisplay" id="fileToDisplay">
    <input type="submit" value="Display Image" name="submit">
</form>
    </body>
</html>

This is the second script

<?php

    $db = mysqli_connect("localhost","root", "", "myDB");

    $sql = "SELECT * FROM images";

    $result = mysqli_query($db, $sql);
    while ($row = mysqli_fetch_array($result)){
        echo "<img src=' images/". $row['image']."' >";
        echo "<p>". $row['text']."</p>";
        echo "</div>";
    }

enter image description here

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The simplest way of doing this is to upload the image to the server and save the image name in your database.

Uploading the image to the server and saving to the database

// display.php
$tmp_file = $_FILES['fileToDisplay']['tmp_name']; // get the temp name of the image
$name = $_FILES['fileToDisplay']['name']; // get the name of the image

Now you need to move the file to a directory on your server with move_uploaded_file() and save the name of the file in the database.

move_uploaded_file($tmp_file, 'images/' . $name)
$remove_extension = explode('.', $name);
$sql = "INSERT INTO `images`(`image`, `text`) VALUES ('$name', '$remove_extension[0]')";

Complete code

// display.php
$tmp_file = $_FILES['fileToDisplay']['tmp_name'];
$name = $_FILES['fileToDisplay']['name'];

if (move_uploaded_file($tmp_file, 'images/' . $name)) {
    $remove_extension = explode('.', $name);
    $sql = "INSERT INTO `images`(`image`, `text`) VALUES ('$name', '$remove_extension[0]')";
   if (mysqli_query($db, $sql)) {
       $last_id = $db->insert_id;
       $sql = "SELECT * FROM images WHERE id = $last_id";

       $result = mysqli_query($db, $sql);
       while ($row = mysqli_fetch_array($result)){
           echo "<div><img src=' images/". $row['image']."' >";
           echo "<p>". $row['text']."</p>";
           echo "</div>";
       }
   }
}

Handling file uploads

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda