Show image from the server folder and display it in a table with data from the MySQL database

2

I'm doing a project in which data is sent using a PHP form to a MariaDB database. I would like to suggest how to show the table with the data of the database as a report and show the image that was stored in the images folder.

The script for the table is as follows:

<?php

$conexion=mysqli_connect(
'localhost',
'root',
'',
'INVENTARIO'
);
if($conexion==FALSE) {
    echo('Error en la conexión');
    exit();
}

$resultado=mysqli_query(
$conexion,
'SELECT*FROM CONTRALORIA'
);  
if ($resultado==FALSE){
echo('Error en la consulta.');
mysqli_close($conexion);
exit();
}
?>

<table border="4" bgcolor = "#D6EEE2" >

<tr>
<th>Número de Inventario</th>
<th>Descripción del Bien</th>
<th>Número de Serie</th>
<th>Marca</th>
<th>Modelo</th>
<th>Color</th>
<th>Area</th>
<th>Condición</th>
<th>Fecha</th>
<th>Fondo</th>
<th>Cuenta</th>
<th>Valor Factura</th>
<th>Valor Actual</th>
<th>Fotografía</th>
</tr>

<?php


while($fila         =
mysqli_fetch_row($resultado)) {
    printf('<tr>');
    printf(
    "<td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td>",  
    $fila[0], $fila[1], $fila[2] , $fila[3], $fila[4], $fila[5] , $fila[6], $fila[7], $fila[8] , $fila[9], $fila[10], $fila[11] ,$fila[12], $fila[13]
    );
    printf('</tr>');

}
?>

</table>



<?php
mysqli_free_result($resultado);
mysqli_close($conexion);

?>

La ruta de la carpeta donde se almacenan los datos es la siguiente:
<img src="/Inventario/images/<?php echo $imagen; ?>" alt="" width="100px" />

This is the database:

MariaDB [INVENTARIO]> describe CONTRALORIA;
+----------------+--------------+------+-----+---------+-------+
| Field          | Type         | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+-------+
| num_inventario | varchar(15)  | NO   | PRI | NULL    |       |
| desc_bien      | varchar(30)  | YES  |     | NULL    |       |
| num_serie      | varchar(30)  | YES  |     | NULL    |       |
| marca          | varchar(30)  | YES  |     | NULL    |       |
| modelo         | varchar(30)  | YES  |     | NULL    |       |
| color          | varchar(30)  | YES  |     | NULL    |       |
| area           | varchar(30)  | YES  |     | NULL    |       |
| condicion      | varchar(30)  | YES  |     | NULL    |       |
| fecha          | varchar(10)  | YES  |     | NULL    |       |
| fondo          | varchar(15)  | YES  |     | NULL    |       |
| cuenta         | varchar(30)  | YES  |     | NULL    |       |
| factura        | int(11)      | YES  |     | NULL    |       |
| actual         | int(11)      | YES  |     | NULL    |       |
| foto           | varchar(250) | YES  |     | NULL    |       |
+----------------+--------------+------+-----+---------+-------+

Only the table appears but I can not get the data stored in the database displayed next to the images stored in the images folder.

I would greatly appreciate your help!

    
asked by Francesc Aburto 16.08.2017 в 07:22
source

2 answers

1

I recommend these modifications:

  • Use htmlspecialchars before sending the contents of the database fields to the browser.
  • For each image, check if it is empty and if it exists.

In the modification I have made, I have commented on each of these steps:

<?php
/* Función de apoyo para convertir los campos en HTML */
function pasar_a_html(&$valor) {
  $valor = htmlspecialchars($valor);
}
/* Ruta relativa desde la posición del script PHP CON barra final
  para facilitar la concatenación */
$ruta_base = 'Inventario/images/';
while ($fila = mysqli_fetch_row($resultado)) {
    /* Convertimos a HTML todas las filas para poder enviarlas al
      navegador de forma segura */
    $html = $fila;
    array_walk($html, 'pasar_a_html');
    /* Si no hay imagen almacenada en la base de datos avisamos de ello */
    if (trim($fila[13]) != '') {
        /* Si existe la imagen la mostramos, en caso contrario avisamos de ello */
        if (file_exists($ruta_base . $fila[13])) {
            $html[13] = '<img src="' . htmlspecialchars($ruta_base . $fila[13]) . '" />';
        } else {
            $html[13] = '<span style="color: red;">(imagen no encontrada)</span>';
        }
    } else {
        $html[13] = '(sin imagen)';
    }
    printf(
        "<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><tr>",
        $html[0], $html[1], $html[2], $html[3], $html[4], $html[5], $html[6],
        $html[7], $html[8], $html[9], $html[10], $html[11], $html[12], $html[13]
    );
}

As you can see in the printf I have only provided the data that has been passed by htmlspecialchars and for each row I detect if there is and the image exists to show one of the three options:

  • (without image): No image (NULL or empty string) is configured in the database.
  • (image not found): In the database there is an image that is not in the image directory.
  • Image: If everything is correct, the image will be displayed in the corresponding cell.
answered by 16.08.2017 в 14:43
0

Hello, you should have a field where you can save the route where the image is hosted, and then require that field with the route data.

It would be:

INSERT TOGETHER WITH YOUR DATA, THE DATA WHERE YOUR IMAGE IS STAYED

$ path="main_folder / images / image.jpg";

And then retrieve that field from your database.

SELECT ALL THE DATA, TOGETHER THESE SHOULD BE THE ROUTE. $ path = field "photo";

AS SOON AS YOUR CODE:

The path of the folder where the data is stored is as follows:

img src="/ Inventory / images / php echo $ image;" ...

- > php echo $ image; - > Where do you generate this variable?

img src="-this bar is over- * / * Inventory / images / ..." ...

In your database you have a photo field. That field I suppose that the route where you lodge the image in the folder of your project should be saved.

I hope to be clear, if you have any doubt I am attentive. If I misunderstood your query, I hope you clarify it.

Greetings .-

    
answered by 16.08.2017 в 14:12