Jump to content

PHP picture name from sql not loading


exceedinglife
 Share

Recommended Posts

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;
        }
    }
?>

 

Link to comment
Share on other sites

17 minutes ago, requinix said:

"Escaping" means like adding a backslash or some other magical character. That there is just switching to using concatenation instead of putting the variable inside the string ("interpolation").

Ok, technically speaking yeah. What I meant was the OP broke free of the the single quotes with concatenation "escaping" from being within the single quotes.

Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

×
×
  • 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.