douju2474 2015-06-19 06:33
浏览 72
已采纳

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
    }
?>
  • 写回答

1条回答 默认 最新

  • dongqi7631 2015-06-19 06:42
    关注

    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']."' ");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了