简体   繁体   English

无法显示来自MySQL的图像

[英]Unable to Display Images from MySQL

I am unable to display images from MySQL. 我无法显示来自MySQL的图像。 I've checked path etc. but still there are broken images on site. 我已经检查过路径等,但网站上仍然有损坏的图像。 Below is my Code. 下面是我的代码。 Please guide me. 请指导我。

<div id="right_content">
    <div id="headline">
    <div id="headline_content"> <b>Welcome Guest!</b> <b style="color:#F56013">Shopping Cart</b> <span>-Items: -Price: </span> </div>
</div>
<div id="products_box">
    <?php
        $get_products ="SELECT * FROM products ORDER BY rand() LIMIT 0,6";

        $run_products = mysqli_query($con,$get_products);

        while($row_products=mysqli_fetch_array($run_products)){
            $prod_id = $row_products['product_ID'];
            $prod_title = $row_products['product_title'];
            $prod_cat = $row_products['category_ID'];
            $prod_brand = $row_products['brand_ID'];
            $prod_price = $row_products['product_price'];
            $prod_desc = $row_products['product_desc'];
            $prod_img =$row_products['product_img1'];



            echo "
            <div id='single_product'>
            <h3>$prod_title</h3>
            <img src='admin_area/product_images/$prod_img' width='180' height='180' />
            </div>
            ";
        }
        ?>
</div>

As per comment, problem is solved. 根据评论,问题已解决。 The images were not moving into the directory and i didn't check that. 图像没有移动到目录中,我没有检查。 When i manually copied them into the specified directory the problem was gone. 当我手动将它们复制到指定目录时,问题就消失了。

When you write html please note that you should always use double and not single quotes for tag properties. 编写html时,请注意,标记属性应始终使用双引号而不是单引号。 So for example 所以举个例子

src='admin_area/product_images/$prod_img'

should be 应该

src="admin_area/product_images/$prod_img"

and so on. 等等。 Since you are doing echo using double quotes you should escape double quotes with . 由于您使用双引号进行回显,因此应使用来对双引号进行转义。 So your echo should look like: 因此,您的回声应如下所示:

echo "
    <div id=\"single_product\">
    <h3>$prod_title</h3>
    <img src=\"admin_area/product_images/$prod_img\" width=\"180\" height=\"180\" />
    </div>
";

IMHO it is better to write plain html and use php only to echo variables just to avoid this kind of problems with the generated code. 恕我直言,最好编写纯HTML并仅使用php来回显变量,以避免生成的代码出现此类问题。 So I'd write: 所以我写:

?>
        <div id="single_product">
        <h3><?php echo $prod_title; ?></h3>
        <img src="admin_area/product_images/<?php echo $prod_img; ?>" width=\"180\" height=\"180\" />
        </div>

<?php } ?>

This will save you a lot of headache in case of debugging if your code is not working 如果您的代码无法正常工作,这将在调试时为您省去很多麻烦

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM