2

So I'm trying to display images from a database for a website that I'm building for a friend. He wants to be able to upload an image, and have it display on the front page.

I have the image uploading all working. Saving it in the database as a BLOB.

I began working on displaying the images on a separate file. I got it working on my test website, images displayed how he wanted them. But then I moved the code over to the actual website, got it to how it needed to be and it didn't work.

I ended up trying it again on my test website, and it all worked fine.

For some, it isn't working on the actual website, but it's working fine on my test website.

The actual website grabs more information the my test website does.

Here is the code the I've used on my test website.

image.php

$query = mysql_query("SELECT * FROM images LIMIT 1");
while($row = mysql_fetch_assoc($query)){

    $image_id = $row['id'];
    echo "<img src=showimage.php?id=".$image_id.">";
}

Here is how I'm grabbing the image from the database and displaying it.

showimage.php

include 'inc/db.php';

$id = addslashes($_REQUEST['id']);

$image = mysql_query("SELECT image FROM sites WHERE id = '$id'");
$image = mysql_fetch_assoc($image);
$image = $image['image'];

header("Content-type: image/png");

echo $image;

Using that code on the test website works fine, there's no problem there.

Here is my code for the actual website.

sites.php

$select = mysql_query("SELECT * FROM sites");
            while($row = mysql_fetch_assoc($select)){

                $image_id = $row['id'];

                echo '
                    <tr class="bottom"><td>'.$row['host'].'</td>
                    <td>'.$row['currency'].''.$row['price'].' every '.$row['payment'].'</td>
                    <td>'.$row['domain'].'</td>
                    <td>'.$row['paid_currency'].''.$row['paid_domain'].'</td>
                    <td>'.$row['features'].'</td>
                    <td>
                        <span title="Edit '.$row['id'].'"><a href="edit.php?id='.$row['id'].'"><img src="images/edit.png" alt="Edit"></a></span>
                        <span title="Delete '.$row['id'].'"><a href="inc/delete.php?id='.$row['id'].'"><img src="images/delete.png" alt="Delete"></a></span>
                    </td>
                    <td><img src=showimage.php?id='.$image_id.'></td></tr>
                    ';
            }

That using the showimage.php file to grab the image from the database, but isn't working at all.

5
  • 7
    Why don't you simply save the image as a separate file and store it's path in the database which can then be used in the src ? Commented Jul 21, 2013 at 11:28
  • 2
    It's possible that some warning message or similar from showimage.php is corrupting the image. If you remove the content type header what output do you see on the real site? Commented Jul 21, 2013 at 11:29
  • 1
    how you saved image in sites table? Commented Jul 21, 2013 at 11:30
  • 3
    Please do not use a database to store images. Use the file system and then store a reference to the image (like the filename) in the database. Have a setting variable that points to the directory path in where the images are stored. When you fetch the image filename from the database, prepend the path from the settings variable and use it as the src attribute of the image tag. Let the web server do what it is good at - serving images. Commented Jul 21, 2013 at 11:45
  • 1
    Apart from what others have said, I have two more comments. Your src attribute is missing quotations. Also, do not use GET requests for modifying (i.e., deleting) data. Commented Jul 21, 2013 at 11:57

1 Answer 1

1

If you're set on using the database, look into using the imagecreatefrompng() function, depending on how the img was put into the DB.

Also, instead of addslashes(), try doing $id = (int)$_GET['id']; then checking if $id > 0. Finally, +1 on storing images on the filesystem and not as a BLOB.

[And insert mysql_query-is-deprecated-use-the-PDO-extension lecture here]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.