CherryLips Posted May 13, 2014 Share Posted May 13, 2014 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']; ?>"> Quote Link to comment https://forums.phpfreaks.com/topic/288450-proper-syntax-for-inserting-an-image-in-a-while-loop/ Share on other sites More sharing options...
CherryLips Posted May 13, 2014 Author Share Posted May 13, 2014 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);?> Quote Link to comment https://forums.phpfreaks.com/topic/288450-proper-syntax-for-inserting-an-image-in-a-while-loop/#findComment-1479300 Share on other sites More sharing options...
Jacques1 Posted May 13, 2014 Share Posted May 13, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/288450-proper-syntax-for-inserting-an-image-in-a-while-loop/#findComment-1479303 Share on other sites More sharing options...
CherryLips Posted May 13, 2014 Author Share Posted May 13, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/288450-proper-syntax-for-inserting-an-image-in-a-while-loop/#findComment-1479306 Share on other sites More sharing options...
Solution QuickOldCar Posted May 13, 2014 Solution Share Posted May 13, 2014 echo "<td><img src='".$rec['IMAGE']."'></td>"; Quote Link to comment https://forums.phpfreaks.com/topic/288450-proper-syntax-for-inserting-an-image-in-a-while-loop/#findComment-1479309 Share on other sites More sharing options...
CherryLips Posted May 13, 2014 Author Share Posted May 13, 2014 You can't see me, but I look VERY grateful. Quote Link to comment https://forums.phpfreaks.com/topic/288450-proper-syntax-for-inserting-an-image-in-a-while-loop/#findComment-1479310 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.