0

am trying to display images page A but calling them from page B. images are contained in the images folder and database table images my page a code is

<img src="addimage.php?image_id=<?php echo $row["code"]; ?>" />

page B code is

<?php 
require_once "db.php"; 
if(isset($_GET['image_id'])) { 
    $sql = "SELECT image FROM images WHERE code=" . $_GET['image_id'];
    $result = mysqli_query($mysqli, $sql) " . mysqli_error($mysqli)); 
    $row = mysqli_fetch_array($result); 
    echo $row["image"]; 
} 
mysqli_close($mysqli); 
?>
M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
eduket
  • 9
  • 2
  • You got some syntax errors in your code: `mysqli_query($mysqli, $sql) " . mysqli_error($mysqli)); `, did you mean to use `or die(mysqli_error($mysqli));`? – M. Eriksson Jul 08 '20 at 09:21
  • 1
    **Warning!** You are _wide open_ for [SQL injection](https://owasp.org/www-community/attacks/SQL_Injection) attacks! You should use parameterized [prepared statements](https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php) instead of using completely unescaped user data directly in your queries like that. _Never ever ever never_ trust user input. – M. Eriksson Jul 08 '20 at 09:22
  • 1
    What does the `image` column in your database contain? A blob with image data, base64 string with the image data or the image path? If it is a blob, then you should also set the correct content-type header before echoing the contents. – M. Eriksson Jul 08 '20 at 09:23
  • image column contains image name – eduket Jul 08 '20 at 09:26
  • image column actually contains the image path – eduket Jul 08 '20 at 09:29
  • 1
    Then you need to read and output the contents of the file instead of outputting the path. – M. Eriksson Jul 08 '20 at 09:31
  • Images do not get displayed because the server _returns_ a path in text form, images get displayed because the server returns image data. You need to _redirect_ to that path here, so that the browser makes a second request for it, and that request will then actually be answered by the server with the image data. – CBroe Jul 08 '20 at 09:32
  • Does this answer your question? [PHP output file on disk to browser](https://stackoverflow.com/questions/2028898/php-output-file-on-disk-to-browser) – M. Eriksson Jul 08 '20 at 09:32
  • 1
    If the image path already is publicly available through your web server, then you should rather put that path directly into the ` – M. Eriksson Jul 08 '20 at 09:34
  • the reason am doing this is, i have product table which am fetching its contents in php file A the products' images are contained in images table. the products table has code column which is also available in images column. – eduket Jul 08 '20 at 09:41
  • So in that case you could just join the images table to the products table in the original query, and directly get the paths there, and put them into the "src" of your tag. I don't think you need a second script for this. e.g. you should aim to write something like `"/>` in the first place. – ADyson Jul 08 '20 at 10:10
  • will it be possible to allow filters for products table if i use joins? – eduket Jul 08 '20 at 10:43
  • I can't see why not, in most cases. But then again I have no idea what your filtering code does, so it's hard to be certain what your concern might be. – ADyson Jul 08 '20 at 12:41
  • @ADyson, i have joined and its actually working. But the problem is duplicate display of records from both tables. I would like to fetch distinct records especially from the products table. – eduket Jul 08 '20 at 16:40
  • Without seeing the new code you used, and examples of the data in each table, I can't say precisely how you should resolve that. – ADyson Jul 08 '20 at 21:52

0 Answers0