2 Replies - 145 Views - Last Post: 11 February 2019 - 10:10 PM Rate Topic: -----

#1 Exceedinglife   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 202
  • Joined: 01-July 12

PHP picture name from sql not loading

Posted 11 February 2019 - 08:28 PM

Hello everyone,
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;
        }
    }
?>



Is This A Good Question/Topic? 0
  • +

Replies To: PHP picture name from sql not loading

#2 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2768
  • View blog
  • Posts: 10,963
  • Joined: 03-December 12

Re: PHP picture name from sql not loading

Posted 11 February 2019 - 09:01 PM

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



This line won’t work...
Was This Post Helpful? 0
  • +
  • -

#3 Exceedinglife   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 202
  • Joined: 01-July 12

Re: PHP picture name from sql not loading

Posted 11 February 2019 - 10:10 PM

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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1