mysqli获取数组从图像中获取所选id的所有数据
I am storing product data in my database including the image file. I then display all of my products in one page through a foreach loop. If I click on a product it takes me to a page called viewProducts.php . In that page, I pull the information from that ID and show it on that page in more detail. However, for some reason the image file is not changing to that ID's image. The image stays as the first product ID's. All of the other product data is correct and correlates with the product I clicked on to view.
My code for the viewProducts page to show the data is this..
<?php
$result = mysqli_query($con,"SELECT * FROM products");
if($row = mysqli_fetch_array($result)) {
$products[$row['product_id']] = $row;
echo "<img class='sizedimg' src='productpics/".$row['img'] ."' alt='Product Pic'>";
}
?>
If it makes it easier to view, my site is buyfarbest.com . Go to the products page and then click on the first product to see it is correct and then click on any of the others.
Products foreach loop
<?php
// Loop to display all products
foreach($products as $id => $product) {
?>
<div class="item">
<div class="productpiccontainer">
<?php echo "<img class='sizedimg' src='productpics/".$product['img'] ."' alt='Product Pic'>"; ?>
</div>
<p><?php echo "<a href='./viewProduct.php?view_product=$id'>" . $product['product_name'] . "</a>"; ?></p>
<p> <?php echo "$" . $product['price']; ?> </p>
</div>
<?php
}
?>
我将产品数据存储在我的数据库中,包括图像文件。 然后,我通过foreach循环在一个页面中显示我的所有产品。 如果我点击一个产品,它会将我带到一个名为viewProducts.php的页面。 在该页面中,我从该ID中提取信息并在该页面上更详细地显示。 但是,由于某种原因,图像文件未更改为该ID的图像。 图像保留为第一个产品ID。 所有其他产品数据都是正确的,并与我点击查看的产品相关联。 p>
我的viewProducts页面的代码显示数据是这个.. p>
&lt;?php
$ result = mysqli_query( $ con,“SELECT * FROM products”);
if($ row = mysqli_fetch_array($ result)){
$ products [$ row ['product_id']] = $ row;
echo“&lt; img class ='sizedimg'src ='productpics /".$ row ['img']。“'alt ='Product Pic'&gt;”;
}
?&gt;
code> pre >
如果它更容易查看,我的网站是buyfarbest.com。 转到产品页面,然后单击第一个产品以查看它是否正确,然后单击任何其他产品。 p>
产品foreach循环 p>
&lt;?php
//循环显示所有产品
foreach($ products as $ id =&gt; $ product){
?&gt;
&lt; div class =“item “&gt;
&lt; div class =”productpiccontainer“&gt;
&lt;?php echo”&lt; img class ='sizedimg'src ='productpics /".$ product ['img']。“'alt = '产品图片'&gt;“; ?&gt;
&lt; / div&gt;
&lt; p&gt;&lt;?php echo“&lt; a href ='。/ viewProduct.php?view_product = $ id'&gt;” 。 $ product ['product_name']。 “&LT; / A&gt;” 中; ?&gt;&lt; / p&gt;
&lt; p&gt; &lt;?php echo“$”。 $产品[ '价格']; ?&GT; &LT; / p为H.
&lt; / div&gt;
&lt;?php
}
?&gt;
code> pre>
div>
Change your viewProduct.php
if your id is interger do it like this
$result = mysqli_query($con,"SELECT * FROM products where product_id =".$_GET['view_product']);
if your id is not integer to it like this
$result = mysqli_query($con,"SELECT * FROM products where product_id ='".$_GET['view_product']."' ");