How to display a photo from the database?

Asked

Viewed 7,504 times

2

I can’t display the photos I’m sending to the database. I’m making a comment system with photo and managed to send them to the database (mysql). I created my database, then my table. I made the connection and all the items in the table are receiving their data. Only that the photo that is not displayed when I call. Only the name of the photo with the extension appears. How can I display the photo with the other comment data? Follow the error. In the first file I have the form and the php call of the comment. In the second file I receive the data and send to the database: I can call all the data, but the photo doesn’t appear, only your name.

Form:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Classificdos</title>
</head>

<body>
  <div id="container">
    <h1>Faça seu Comentario</h1>

    <?php // aqui inicia a busca de comentarios dentro do banco de dados. require 'conexao.php'; $buscaComentario=m ysql_query( "SELECT * FROM comentarios WHERE identificacao = '1' AND moderacao ='nao'"); while ($lista=m ysql_fetch_array($buscaComentario))
    { $nome=$ lista[ 'nome']; $site=$ lista[ 'site']; $comentario=$ lista[ 'comentario']; $avatar=$ lista[ 'avatar']; echo "
                            <p><strong>NOME: </strong> $nome </p>
                            <p><strong>SITE: </strong> $site </p>
                            <p><strong>COMENTÁRIO: </strong> $comentario </p>
                            <img>$avatar</img>
                            <hr/>

                        "; } ?>
    <hr/>

    <h3>Deixe seu comentário</h3>

    <form id="" action="cadastraComentario.php" method="post" enctype="multipart/form-data">
      <fieldset>
        <legend>Preencha os Campos Abaixo:</legend>

        <label for="nome">NOME:</label>
        <input type="text" required id="nome" name="nome">
        <div class="clear"></div>

        <label for="email">E-MAIL:</label>
        <input type="text" id="email" name="email">
        <div class="clear"></div>

        <label for="site">SITE (Opcional):</label>
        <input type="text" id="site" name="site">
        <div class="clear"></div>
        <label for="comentario">Deixe seu Comentário</label>
        <br/>
        <textarea name="comentario" id="comentario" cols="60" rows="10"></textarea>

        <label id="escolher_foto" for="foto">Escolher uma Foto</label>
        <input type="file" name="avatar" id="avatar">

        <input type="submit" value="Comentar!">
        <br/>
        <input type="hidden" name="identificacao" value="1" />
        <input type="hidden" name="moderar" value="nao" />
      </fieldset>
    </form>

  </div>
</body>

</html>

register:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <div id="seguraConteudo"> 

        <?php
        //error_reporting(0);

        require 'conexao.php';


        $nome =         $_POST['nome'];
        $email =        $_POST['email'];
        $site =         $_POST['site'];
        $comentario =   $_POST['comentario'];
        $identificacao= $_POST['identificacao'];
        $moderacao =    $_POST['moderar'];
        $arquivo =      $_FILES['avatar']['name'];
        $arquivoTemp =  $_FILES['avatar']['tmp_name'];

        $pasta = "imagens/";
// Coloca a foto em uma pasta diretorio
        move_uploaded_file($arquivoTemp, $pasta);


        $headers = "Content-type:text/html; charset=UTF-8";
        $headers = "From: $email";
        $para    = "[email protected]";
            $mensagem = "De: $nome";
            $mensagem .= "E-mail: $email";
            $mensagem .= "Site: $site";
            $mensagem .= "Comentario: $comentario";



        $envia = mail($para, "Comentário Efetuado no site", $mensagem, $headers);


        $insere = ("INSERT INTO comentarios (id, nome, email, site, comentario, identificacao, moderacao,avatar ) VALUES ('NULL', '$nome', '$email', '$site', '$comentario', '$identificacao', '$moderacao', '$arquivo')");

        $insereBanco = mysql_query($insere);

            echo "<p><strong>$nome</strong>, seu comentário foi efetuado com sucesso e aguarda liberação. Obrigado!";
            echo "<p><a href='Sistema_comentarios.php'>Voltar</a></p>";

        ?>
    </div>

</body>
</html>

1 answer

1


From what I can see in your code, you’re doing:

$avatar=$lista[ 'avatar'];

<img>$avatar</img>

How you are storing an image, the contents of the variable $avatar is a url, so you should do so:

<img src=$avatar></img>

From what I understand it’s this.


Your problem is in the path tree for the images, in the database only has a string that is the name and extension, so an example of file organization:

This is the contents of the directory pastadomeuprojeto

-- index.html
-- pagina01.html
-- pagina02.html
-- /images
   -- logo.png
   -- decoracao.png
   -- frutas.jpg
-- /css
   -- home.css
-- /js
   -- effects.js

In general this is the organization of files of a web project, if I want frutas.jpg inside my page01.html page, my url will be /images/frutas.jpg, if it doesn’t work it can be without the first bar /, if it still doesn’t work use full path:

C://../pastadomeuprojeto/images/frutas.jpg

  • 1

    Thanks for the leandro answer, I tried it then but it didn’t work out, but I believe it is there, at least now appears that little error image, that there is an image but it was not displayed. There’s something I’m missing and I don’t know.

  • Print the contents of your $avatar variable, probably the image path will be wrong, try using the absolute path or something like

  • I gave an echo "$avatar"; and again only the name of the image appeared, the same way it is in the database.

  • you have to complete the path with the folders it is in, if you only use its name, it has to be in the root of the html document that you make this request

  • I made this code coming from two different tutorials, I am beginner and I rent something I need help. Descilpa anything then. I created the variable $folder = "images/", I do not know how to find this path now and nor. how it would look?

  • edited the answer see if it helped

  • perfect leandro I noticed well, but in my folder of my project there is no image folder, just appeared a file called image, type file, no extension with 3MB. I would have to have created a folder named image like this when I created the $folder ="image/"variable? Where are the photos now that I sent to the bank? I can not find them, to seek the way. the strange is this file I told you called image that appeared in the folder of this project

  • You probably have to configure the $_FILES[] variable, which receives the file in the upload, to look at this link: http://tableless.com.br/upload-de-arquivos-com-php/

  • Thank you Leandro this tutorial solved the problems I had in the code, the photo is being replaced and all the time he taught to change the name. And the img should be displayed like this <img src='uploads/$avatar'>

  • I’m glad I could help :)

Show 5 more comments

Browser other questions tagged

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