-2
I am trying to display the images of products registered in the database, each product has its own image.
<body>
<div class="container theme-showcase" role="main">
<div class="page-header">
<h1 style="color: #fff;"><center>Catálogo de Produtos</center></h1>
</div>
<div class="row">
<?php while($rows_produtos = mysqli_fetch_assoc($resultado_produtos)){ ?>
<div class="col-sm-6 col-md-3">
<div class="thumbnail" style="border-radius: 10px 10px 10px 10px;">
<img src="../img/produtos/30-08-2021-14-43-43-88811699.jpg" width="100" height="100" id="target">
<?php if(@$foto != ""){ ?>
<img src="../img/produtos/<?php echo $foto ?>" width="100" height="100" id="target">
<?php }else{ ?>
<img src="../img/produtos/sem-foto.jpg" width="100" height="100" id="target">
<?php } ?>
<div class="caption text-center">
<h3>Produto <br><?php echo $rows_produtos['nome']; ?></h3><br><h3>Descrição <br><?php echo $rows_produtos['descricao']; ?></h3><br><h3> Preço: R$ <?php echo $rows_produtos['valor_venda']; ?></h3><h3>Código: <?php echo $rows_produtos['codigo']; ?></h3>
</a>
</div>
</div>
</div>
<?php } ?>
</div>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
</body>
to perform the sql query I am using the following code:
<?php
$pag = 'produtos';
include_once("conexao.php");
$result_produtos = "SELECT * FROM produtos where estoque >= 1 ";
$resultado_produtos = mysqli_query($conn, $result_produtos);
?>
OBS: the image registration system is already working, I can even search all the information registered in the database, I just can’t get the images of the products, each product has its own image.
if I place the absolute path: it shows the image registered in the database, however it adds the same image for all products, I would like to know what I am doing wrong?
//SCRIPT PARA SUBIR FOTO NO BANCO
$name_img = date(’d-m-Y H:i:s') . '-'. @$_FILES['image']['name']; $im_name = preg_replace('/[ :]+/' , '-' , $im_name);
$path = '.. /.. /img/products/' . $im_name; if (@$_FILES['image']['name'] == ""){ $image = "no-photo.jpg"; }Else{ $image = $im_name; }
$imagem_temp = @$_FILES['image']['tmp_name'];
$ext = pathinfo($image, PATHINFO_EXTENSION);
if($ext == 'JPG' or $ext == 'jpg' or $ext == 'jpeg'){
move_uploaded_file($imagem_temp, $path);
}Else{
echo 'Image Extension not allowed, use jpg image only!! ';
Exit();
}
if($id == ""){ $res = $Pdo->prepare("INSERT INTO products SET code = :code, name = :name, description = :description, sale = :value_sale, category = :category, photo = :photo, stocking_min = '$estoque_min'"); $res->bindValue(":code", $code); $res->bindValue(":name", $name); $res->bindValue(":Description", $Description); $res->bindValue(":valor_venda", $valor_venda); $res->bindValue(":category", $category); $res->bindValue(":photo", $image); $res->execute(); }These{
if($imagem != 'sem-foto.jpg'){
$res = $pdo->prepare("UPDATE produtos SET codigo = :codigo, nome = :nome, descricao = :descricao, valor_venda = :valor_venda, categoria = :categoria, foto = :foto, estoque_min = '$estoque_min' WHERE id = :id");
$res->bindValue(":foto", $imagem);
}else{
$res = $pdo->prepare("UPDATE produtos SET codigo = :codigo, nome = :nome, descricao = :descricao, valor_venda = :valor_venda, categoria = :categoria, estoque_min = '$estoque_min' WHERE id = :id");
}
$res->bindValue(":codigo", $codigo);
$res->bindValue(":nome", $nome);
$res->bindValue(":descricao", $descricao);
$res->bindValue(":valor_venda", $valor_venda);
$res->bindValue(":categoria", $categoria);
$res->bindValue(":id", $id);
$res->execute();
}
echo 'Saved with Success!'; ?>
Could format code for better display?
– Danizavtz
Please edit the question to limit it to a specific problem with sufficient detail to identify an appropriate answer.
–
sorry I’m a beginner here in the community and I don’t know how to use stackoverflow tools very well
– marcosramos86
For all products you say that the image is the variable
$foto
, well done<img src="../img/produtos/<?php echo $foto ?>" />
. Being the same variable, it will be the same photo for all products. This variable should change product to product?– Woss
A little earlier in the code you put
<img src="../img/produtos/30-08-2021-14-43-43-88811699.jpg" />
, which is a static image, which will also be the same for all products.– Woss
@Woss each product has an image, this static image is just to show that it is accessing the img/products folder, the $photo variable is the same for all products, I will show how I am saving the image path in the bank.
– marcosramos86
"it shows the image registered in the database, however it adds the same image for all products, I would like to know what I am doing wrong", see comment above.
– Woss
@Woss I discovered the image path as follows, <H3><? php echo $rows_products['photo']; ? ></H3> now I can’t get this link to show the image.
– marcosramos86
Why not try to replace the variable
$foto
by that variable there?– Woss
@Woss solved it as follows: <td><center><img src=".. /img/<? php echo $pag ? >/<? php echo $rows_products['photo']; ? >" width="40"></center></td> thanks for the help
– marcosramos86