0

This is my .php file

<?php
        ini_set('mysql.connect_timeout',300);
        ini_set('default_socket_timeout',300);
    ?>
    <html>
        <body>
            <form method="post" enctype="multipart/form-data">
            <br/>
                <input type="file" name="image" />
                <br/><br/>
                <input type="submit" name="sumit" value="Upload" />
            </form>
            <?php
                if(isset($_POST['sumit']))
                {
                    if(getimagesize($_FILES['image']['tmp_name']) == FALSE)
                    {
                        echo "Please select an image.";
                    }
                    else
                    {
                        $image= addslashes($_FILES['image']['tmp_name']);

                        $image= file_get_contents($image);
                        $image= base64_encode($image);
                        saveimage($image);
                    }
                }
                displayimage();
                function saveimage($image)
                {
                    $con=mysql_connect("localhost","root","");
                    mysql_select_db("food",$con);
                    $qry="insert into info (image) values ('$image')";
                    $result=mysql_query($qry,$con);
                    if($result)
                    {
                        echo "<br/>Image uploaded.";
                    }
                    else
                    {
                        echo "<br/>Image not uploaded.";
                    }
                }

                function displayimage()
                {
                    $con=mysql_connect("localhost","root","");
                    mysql_select_db("food",$con);
                    $qry="select image from info";
                    $result=mysql_query($qry,$con);
                    while($row = mysql_fetch_assoc($result))
                    {
                        echo '<img height="300" width="300" src="data:image;base64,'.$row[2].' "> ';
                    }
                    mysql_close($con);   
                }
            ?>
        </body>
    </html>

Why i cannot display my picture when i add it. The picture is stored in the database already when i click upload but i cant display it. Is there anything wrong with the display code there?Thanks

ron
  • 41
  • 7
  • 2
    **Stop** using deprecated `mysql_*`API. Use `mysqli_*` or `PDO` instead. Also check for Errors after executing an SQL query – Jens May 19 '16 at 08:17
  • 1
    First of all. stop using `mysql_*`, those functions are depecated and really unsafe. 2nd: what happens, when you dump `$row` ? – Peon May 19 '16 at 08:20

2 Answers2

1

In your current code change here

Replace $row[2] to $row['image']

And Try to convert your code as commented suggestion.

Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
0

See this line,

while($row = mysql_fetch_assoc($result)){ ...

You're fetching the row as an associative array, not as a numeric array, so $row[2] won't work. Use $row['image'] in your <img> element to display the image.

So your code should be like this:

while($row = mysql_fetch_assoc($result)){
    echo '<img height="300" width="300" src="data:image;base64,'. $row['image'] . '">';
}

Sidenotes:

  1. Don't use mysql_* functions, they are deprecated as of PHP 5.5 and are removed altogether in PHP 7.0. Use mysqli or pdo instead. And this is why you shouldn't use mysql_* functions.
  2. Learn about prepared statements because right now your queries are susceptible to SQL injection. Also see how you can prevent SQL injection in PHP.
Community
  • 1
  • 1
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37