Search code examples
phpimageshopping-cart

PHP picture name from sql not loading


I have a PHP shopping cart I am working on. I have all my pictures stored in a folder url directory of my project. In my SQL database I have an image table that holds all of the image names. When I load the picture names with my php It somehow adds some extra random characters to the directory path: /img/%7B$row_product_image[name]%7D 404 (Not Found) If I hardcode the image directory img/picturename.jpg It works. Here is what I have.

<?php
include_once "objects/database.php";
include_once "objects/product.php";
include_once "objects/product_images.php";
// object instances
$database = new Database();
$db = $database->getConnection();
$product = new Product($db);
$product_image = new ProductImage($db);
$recordsPerPage = 1;
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    extract($row);
    echo '<div class="col-md-4 mb-2">';
        echo '<div class="product-id d-none">{$id}</div>';
            echo '<a href="product.php?id={$id}" class="product-link">';
                $product_image->product_id = $pid;
                $stmt_product_image = $product_image->readFirst();
                while($row_product_image = $stmt_product_image->fetch(PDO::FETCH_ASSOC)) {
                    echo '<div class="mb-1">';
                        echo '<img src="img/{$row_product_image[name]}" class="w-100" />';
                    echo '</div>';
                }
                echo '<div class="product-name mb-1">{$name}</div>';
            echo '</a>';

    echo '</div>';
}
class ProductImage {
    private $pdoConn;  private $table_name = "product_images";
    public $id;
    public $product_id;
    public $name;
    public $timestamp;
    public function __construct($dbconn) {
        $this->pdoConn = $dbconn;
    }
    function readFirst() {
        // SELECT query
        $query = "SELECT id, pid, name " .
                 "FROM " . $this->table_name . " " .
                 "WHERE pid = ? " .
                 "ORDER BY name DESC " .
                 "LIMIT 0, 1";
        // prepare query statement
        $stmt = $this->pdoConn->prepare($query);
        // sanitize
        $this->product_id=htmlspecialchars(strip_tags($this->product_id));
        // bind variable as parameter
        $stmt->bindParam(1, $this->product_id);
        // execute query
        $stmt->execute();
        // return values
        return $stmt;
    }
}
?>

Solution

  • echo '<img src="img/{$row_product_image[name]}" class="w-100" />';
    

    PHP will NOT expand variables if string is quoted using single quotes (docs). Also you need to quote "name" string to use it as array index (docs).

    That said, either fix that by avoiding single quotes (but that's ugly, so don't):

    echo "<img src=\"img/{$row_product_image['name']}\" class=\"w-100\" />";
    

    or less painful to read:

    echo '<img src="img/' . $row_product_image['name'] . '" class="w-100" />';
    

    or even use printf() to avoid spaghetting the string:

    printf('<img src="img/%s" class="w-100" />', $row_product_image['name']);