dongyun7571 2016-12-02 06:38
浏览 81

如何解决上传到数据库后使用php显示的破碎图像

I try upload image to mysql database and display it along with the description of image using php. After i upload the image and display it , a broken image was displayed but the description of the image was displayed without any error. How can I solve this problem ? Appreciate your help

<?php

    $msg = "";
    //if upload button is pressed
    if(isset($_POST['upload']))     
    {
        // the path to store the uploaded image
        $target = "images/".basename($_FILES['image']['name']);

        // connect to database
        $db = mysqli_connect("localhost","root","","product");

        // Get all the submitted data from the form
        $image = $_FILES['image']['name'];
        $text = $_POST['text'];

        $sql = "INSERT INTO product_list (image, text) VALUES ('$image','$text')";
        mysqli_query($db,$sql); // stores the submitted data into the database table : product_list

        // move uploaded image to the folder : image
        if (move_uploaded_file($_FILES['image']['tmp_name'],$target))
        {
            $msg = "Image and text uploaded successfully";
        }else
        {
            $msg = "There was a problem uploading image";
        }
    }

?>

<!DOCTYPE html>
<html>
<head>
<title>Image Upload With Description</title>
<link rel="stylesheet" type="text/css" href="formstyle.css">
</head>
<body>
<div id="content">
<?php
    $db = mysqli_connect("localhost","root","","product");
    $sql = "SELECT * FROM product_list";
    $result = mysqli_query($db, $sql);
    while ($row = mysqli_fetch_array($result))
    {
        echo "<div id='img_div'>";
            echo "<img src='".$row['image']."'>";
            echo "<p>".$row['text']."</p>";
        echo "</div>";
    }
?>
    <form method="post" action="try.php" enctype="multipart/form-data">
        <input type="hidden" name="size" value="1000000">
        <div>
            <input type="file" name="image">
        </div>

        <div>
            <textarea name="text" cols="40" rows="4" placeholder="Details of product"></textarea>
        </div>

        <div>
            <input type="submit" name="upload" value="Upload Image">
        </div>
    </form>
</div>
</body>
</html>

This is my result :

enter image description here

  • 写回答

2条回答 默认 最新

  • dongyi5425 2016-12-02 07:04
    关注

    You are storing it in the DB without the images directory. You either need to store it with that, or always remember to call it that way in your image calls.

    echo "<img src='images/".$row['image']."'>";
    

    or make your the record you are writing the same as the filesystem location.

    $image = 'images/' . $_FILES['image']['name'];
    

    Note you are open to SQL injections and file inclusion injections with this code.

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?