How to display the BD image in php?

Asked

Viewed 55 times

2

I’m making a website for sales (personal thing, just training) and my problem happens with displaying the loaded image of the comic. The browser identifies that it has an image there, but it does not show the same.

Send the image by $_FILES in HTML

    <html>
    <head>
        <title></title>
        <meta charset="utf-8/">
    </head>
    <body>
          <form name="enviaImg" action="salvaImg.php" method="POST"  enctype="multipart/form-data">
                <input type="file" name="imagem" value=""/>
                <p>Categoria: <input type="text" name="eCategProd"/></p>
                <p>Descricao: <input type="text" name="eDescricao"/></p>
                <p>Preco: <input type="text" name="ePreco"/></p>
                <input type="submit" name="envia" value="Salvar"/>
          </form>
   </body>
   </html>

save the image in PHP with the $_FILES:

    <?php
          $nomeProd = $_FILES["imagem"]["name"];
          $temp = $_FILES["imagem"]["tmp_name"];
          $categProd = $_POST["eCategProd"];
          $precoProd = $_POST["ePreco"];
          $descProd = $_POST["eDescricao"];

          $conexao = mysqli_connect('localhost', 'root', '', 'ecomoveis');
          $insere = mysqli_query($conexao, "INSERT INTO tb_produto (prod_Nome, prod_Descricao, prod_Categoria, prod_Url, prod_Preco) VALUES ('{$nomeProd}', '{$descProd}', '{$categProd}', '{$temp}', '{$precoProd}')") or die(mysqli_error($conexao));

          move_uploaded_file($temp, "../EcoMoveis/Moveis/".$nomeProd);

          header("location: enviar.php");
      ?>

I read and organize in one vector and display in another PHP page:

     <?php 
         $conexao = mysqli_connect('localhost', 'root', '', 'ecomoveis');
         $consulta = mysqli_query($conexao, "SELECT * FROM tb_produto WHERE prod_Categoria = 'Cadeira'");

         while($linha = mysqli_fetch_array($consulta))
         {
            $produto[] = $linha;
         }
     ?>

And finally I show off in some field inside <body>

   <html>
      <body>
         <div class="produto-imagem">
              <?php 
                 include("../PHP/Cadastro de Produtos/pegaImg.php");
                 foreach($produto as $prod)
                 {
              ?>
              <img src="<?php echo "../EcoMoveis/Moveis/".$prod["prod_Nome"] ?>"/>
              <?php 
                 }
              ?>
        </div>
     </body>
  </html>

In case there are 2 images with the category "Chair", then it puts 2 squares (same as those that appears when you do not recognize the image) but nothing of the image appear

  • Actually you’re not saving the image in the comic book, just her name.

  • @Leo Caracciolo, then I managed to make her appear on the same page ("enviaImg.php"), but I need it to appear on another page. php and that’s where the thing curls up, because it identifies that it has an image there, it just does not appear the image

  • @Sam, could you show me a way to save the image in the comic book?

  • If the images have the same path, what you will do is save the name, for example: imagem1.jpg

  • @Guilhermecosta the image is saved in a folder on the server so to appear on any page you have to put in the attribute src image the relative address of the image

  • @Leocaracciolo then, I was able to solve the problem, it was because the file that picks up the image in the bd and the file that shows the image was in different folders. Thank you for your attention, sorry about anything/

  • https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079

  • there is no way I can mark "answer accepted" in comment

  • Oops, I know, it was just to let you know that there is such a thing as marking answer as accepted.

  • Failed to set the file extension. Tip: Whenever saving paths with file names in the database, save the file name with the extension.

Show 5 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.