0

I'm making an order information page, and I would like to retrieve the image of product that customer order. Currently, in my page it can show the customer's product name, type price, etc. except the image. I've tried to retrieve the image but the code I use below is not working.. I really need your help to tell me which part could have gone wrong. Thank you very much.

here's the form.php

<table style="border-collapse: collapse; width: 100%; border-top: 1px solid #DDDDDD; border-left: 1px solid #DDDDDD; margin-bottom: 20px;">
<thead>
  <tr>
    <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: left; padding: 7px; color: #222222;"><?php echo $text_product; ?></td>
    <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: left; padding: 7px; color: #222222;"><?php echo $text_model; ?></td>
    <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: right; padding: 7px; color: #222222;"><?php echo $text_quantity; ?></td>
    <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: right; padding: 7px; color: #222222;"><?php echo $text_price; ?></td>
    <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: right; padding: 7px; color: #222222;"><?php echo $text_total; ?></td>
  </tr>
</thead>
<tbody>
  <?php foreach ($products as $product) { ?>
  <tr>
    <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; text-align: left; padding: 7px;">
<img src="getImage.php?$product_id=$product_id" width="100" height="100" /><?php echo $product['name']; ?>
      <?php foreach ($product['option'] as $option) { ?>
      <br />
      &nbsp;<small> - <?php echo $option['name']; ?>: <?php echo $option['value']; ?></small>
      <?php } ?></td>
    <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; text-align: left; padding: 7px;"><?php echo $product['model']; ?></td>
    <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; text-align: right; padding: 7px;"><?php echo $product['quantity']; ?></td>
    <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; text-align: right; padding: 7px;"><?php echo $product['price']; ?></td>
    <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; text-align: right; padding: 7px;"><?php echo $product['total']; ?></td>
  </tr>

and this is the getImage.php

<?php
$id = $_GET['id'];

$link = mysql_connect("localhost", "root", "");
mysql_select_db("nirvania");
$sql = "SELECT image FROM db_product WHERE product_id=$product_id";
$result = mysql_query("$sql");
$row = mysql_fetch_assoc($result);
mysql_close($link);

header("Content-type: image/jpeg");
echo $row['image'];
?>

2 Answers2

0

In your getImage.php you are checking for a key id in the query string, but you are passing something like 1=1. Change your HTML with:

<img src="getImage.php?id=<?php echo $product_id; /* or $product['id'] */ ?>">

However, this is not the best approach to store images directly in the database. I advice you to add a image_path column to your product table, so you can simply do:

<img src="<?php echo $product['image_path']; ?>">
Gianluca Mancini
  • 1,302
  • 9
  • 10
0

couple of things you should look into, your image source is using php variable but it is not inside php tags

did you try changing your query from

SELECT image FROM db_product WHERE product_id=$product_id

to

SELECT image FROM db_product WHERE product_id=`$product_id`
Shairyar
  • 3,268
  • 7
  • 46
  • 86