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 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图