0

Screenshot of the code: screenshot

if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    require ('../../mysqli_oop_connect.php'); //Connect to the database.
    $errors = array(); // Initialize an error array.

// Check for an isbn:
if (empty($_GET['isbn'])) {
    $errors[] = 'You forgot to enter the ISBN-13.';
} else {
    $isbn = $mysqli->real_escape_string(trim($_GET['isbn'])); 
}   

if (empty($errors)) {   
    //Make the query:
    $q = "SELECT * FROM books WHERE isbn=$isbn";
    $r = $mysqli->query($q);
    $num = $r->num_rows;

    if ($num == 0) { // If it fails to run:
        echo "<br /><br /><p>This book is currently not listed in our database. Would you like to add it?</p>\n";
        //HERE IS WHERE IT NEEDS TO REDIRECT TO A PAGE THAT ALLOWS THE CONTENT TO BE ADDED TO THE DB LIKE REGISTER.PHP DOES.
    } else { //If it suceeds then run the query:
        echo '<h1>Is this your book?</h1>';

    while ($row = $r->fetch_object()) { // POST DATA FROM DB.
        echo '<img src="$row->image" height="100" width="100">' . '<br />' . $row->name . '<br />' . $row->isbn ;

The rest of the code is just closing the DB connection.

I don't know what that icon means or where the issue is in the code. The DB is setup with three columns in a table: image, isbn, and name. The goal is to get the book to display the data being retrieved from the database. It is displaying all three items from the database based on the query, except the image is something else. The HTML is recognizing that there is an image being retrieved because when I just do $row->image it freaks out and posts random nonsense, as it should when there is an image being displayed like that. Any help is greatly appreciated.

5
  • that image means your image is not found, check your image path Commented Dec 30, 2013 at 1:38
  • Can you inspect and open the image path in new tab/window if it is not found, means the path is incorrect Commented Dec 30, 2013 at 1:39
  • If your image is stored in your database either: you'll need to create a script to retrieve it and serve it with the appropriate header information and put a URL in your page that points to that script; or, create a data URL on the fly from the data you retrieve from the database. Commented Dec 30, 2013 at 1:40
  • You can't use object like that ($row->image) in string. Should be {$row->image} Commented Dec 30, 2013 at 1:42
  • The issue was I am storing images in the DB, not in a folder. I am told that going that route is not "secure", but I don't honestly see why it isn't secure. To me, by storing the images on the DB and not in the uploads folder it is seemingly more efficient, but I can be wrong. Commented Dec 30, 2013 at 18:32

3 Answers 3

3
echo '<img src="'.$row->image.'" height="100" width="100"><br />' . $row->name . '<br />' . $row->isbn ;

Try this way, and better you print your image path and check if it does exist or does not exist, and yes you can also inspect image element and check path of the image.

Its not clear in the question whether there is image link saved in the db or image data in base64. Answer is given assuming db has image link.

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

3 Comments

Explaining why this works, and why OP's code does not, would be helpful. ;)
I tried that, but when I break out of the code like that '<img src="' . $row->image . '" height="100" width="100">' it prints out what the image is in HTML, which is just a bunch of random characters.
You must clear in your question that you have an image link in your db, or img data in base64
1

I'd guess from your comments about 'freaks out and posts random nonsense' that your image is stored in your database, rather than the path to your image. If so, you can't simply echo the image data in your <img> tag. The src attribute requires a URL, which you can create in one of two ways: create a script that serves the image with appropriate headers and use the URL of that script as your src attribute; or, create a data URI and include the data in your page. The latter is probably the simplest, but could increase the load time of your pages. Here's how:

// base64 encode image data and prepend the header
$imgURL = "data:image/png;base64,".base64_encode($row->image);  

echo '<img src="'.$imgURL.'" height="100" width="100">' .
     '<br />' . $row->name . '<br />' . $row->isbn ;

Note - you'll need to change the dataURL header to send the correct image type: image/jpg, image/png, etc.

2 Comments

That worked! Wow! I learned something! Thank you! What is base64_encode, and is there any security concerns that I should be aware of when using that?
base64_encode() reference is here. I can't think of any specific security concerns with this.
0

You cannot have string substitution with single quotes. It will simply place the text $row->image in the echo:

echo '<img src="$row->image" height="100" width="100">' . '<br />' . $row->name . '<br />' . $row->isbn ;

Try this instead:

echo "<img src='$row->image' height='100' width='100'>" . '<br />' . $row->name . '<br />' . $row->isbn ;

Or this:

echo '<img src="' . $row->image . '" height="100" width="100">' . '<br />' . $row->name . '<br />' . $row->isbn ;

Personally, I prefer using sprintf like this:

echo sprintf('<img src="%s" height="100" width="100">', $row->image) . '<br />' . $row->name . '<br />' . $row->isbn ;

But on top of that, to make your debugging life even easier, I recommend formatting your PHP code to be easier to read like this:

echo sprintf('<img src="%s" height="100" width="100">', $row->image)
   . '<br />'
   . $row->name
   . '<br />'
   . $row->isbn
   ;

The cleaner your code is from the beginning, the easier it is to spot flaws & bugs when they arise.

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.