Jump to content

proper syntax for inserting an image in a while loop?


Go to solution Solved by QuickOldCar,

Recommended Posts

I'm trying to insert an image and this is my code:

 

      echo "<tr>";
          echo '<td><input type="submit" name="btnPost" value="'.$rec['SKU'] . '"</td>' ;
          echo "<td>".$rec['NAME']."</td>";
          echo "<td>".$rec['DESCR']."</td>";
          echo "<td>".$rec['PRICE']."</td>";
          echo "<td>"<img src=".$rec['IMAGE']">."</td>";
      echo "</tr>";
        $row++ ;

 

I tried just echoing the .$rec['IMAGE'] without the img tag, but all it does is echo the name of the file instead of presenting the image itself.

 

This is the code I'm using on another page in my HTML and it works fine, but I was curious how to insert the image inside my while loop.

 

        <img src="<?php echo $rec['IMAGE']; ?>">

Here is the code for the table just in case you want to see the whole thing... The line in bold is the one that I can't get to work.

 

 

<?php
  $conn = mysqli_connect("localhost","root","","skel");
  $sql = "SELECT * FROM products ;";
  $result = mysqli_query($conn, $sql);
  $row = 1 ;
  echo '<form name="form1" method="post" action="product.php">' ;
  echo "<table border='1'>";
  while ($rec = mysqli_fetch_assoc($result)) {
      echo "<tr>";
          echo '<td><input type="submit" name="btnPost" value="'.$rec['SKU'] . '"</td>' ;
          echo "<td>".$rec['NAME']."</td>";
          echo "<td>".$rec['DESCR']."</td>";
          echo "<td>".$rec['PRICE']."</td>";

          echo "<td>"<img src=".$rec['IMAGE']">."</td>"
      echo "</tr>";
        $row++ ;
}
echo '</table><br><br>' ;
echo '<input type="submit" name="btnPost" value="Check Out (Shopping Cart)"></form>' ;
mysqli_close($conn);
?>

Hi,

 

you should read my comment in your last thread. Your current way of writing PHP code is extremely insecure and can lead to all kinds of attacks against your server or your users.

 

Do not insert raw values into your HTML markup. Always escape them first. This is crucial!

 

Besides that, I have no idea what you mean by “doesn't work”. Can you be more specific? Right now, all I see is that the syntax is badly broken with randomly placed quotes and missing “.” operators. I think you should simply rewrite this line.

 

But the escaping is much more important.

I appreciate your insight. I am taking php as a required course for my Graphic Design / Web degree in a community college and the first half of the course consisted of the teacher reading powepoints to us. The second half was hands on database stuff and the final project is, "build your own shopping cart." So hopefully this explains why it looks like I have no idea what I'm doing as well as the lack of security. Also the book we got for this class is full of errors and is for php6 which I understand isn't really out. I am a total beginner and I really am thankful for all the help I've been getting here.

 

That being said, I have a table on my products page in my shopping cart and I'd like to be able to display an image of the product at the end of the row for each item. Currently I have the image appearing on the product page in HTML which is where you can see each product individually and add it to the cart. I'd really like to make all the images appear on the products page but I'm not sure of the proper syntax to display the images from my while loop.

 

On another page I have the image appearing when I write the tag in HTML:

<img src="<?php echo $rec['IMAGE']; ?>">

 

When I write the tag for my while loop, Im clearly not using proper syntax because the image won't show up. If all I write is:

echo "<td>".$rec['IMAGE']."</td>"; 

Then just the name of the file appears and not the image itself.

 

I understand there is a lot I still need to learn but I am just trying to get the hang of the basics right now. Thanks for any and all of your help :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.