【问题标题】:Unable to display image after saving it in mysql using php使用php将图像保存在mysql中后无法显示图像
【发布时间】:2014-02-11 09:24:28
【问题描述】:

在 mysql 中将图像保存为 blob 后,我无法显示图像。有人可以帮我调试一下吗:

<?php
    // just so we know it is broken
    error_reporting(E_ALL);
    // some basic sanity checks
    if(isset($_GET['id'])) {
        //connect to the db
        $link = mysql_connect('localhost', 'root', '') or die("Could not connect: " . mysql_error());

        // select our database
        mysql_select_db("poll") or die(mysql_error());

        // get the image from the db
        $sql = "SELECT image FROM polldetails WHERE image_name= $id";

        // the result of the query
        $result = mysql_query("$sql") or die("Invalid query: " . mysql_error());

        // set the header for the image
        header("Content-type: image/jpg");
        echo mysql_result($result, 0);

        // close the db link
        mysql_close($link);
    }
    else {
        echo 'Please use a real id number';
    }
?>

$imgData =addslashes (file_get_contents($_FILES['file']['tmp_name']));
        $size = getimagesize($_FILES['file']['tmp_name']);
            $link = mysql_connect('localhost', 'root', '');
        if (!$link) 
        {
            die('Not connected : ' . mysql_error());
        }
        mysql_select_db ("poll") OR DIE ("Unable to select db".mysql_error());
        $sql = "INSERT INTO polldetails
                    ( poll_id, image_id , image_type ,image, image_size, image_name)
                    VALUES
                    ('1', '11', '{$size['mime']}', '{$imgData}', '{$size[3]}', '{$_FILES['file']['name']}')";

        if(!mysql_query($sql)) 
            {
                    echo 'Unable to upload file';
            }
        else
            {
                    $qry = mysql_query("SELECT * FROM polldetails where image_name = '{$_FILES['file']['name']}' ");

                    while ($row = mysql_fetch_array($qry))
                    {
                        //echo $row['image'];
                        echo "<img src=file.php?id=".$row["image_name"]." height=200 width=200>";   

                    }
            }

**file.php**
<?php
    // just so we know it is broken
    error_reporting(E_ALL);
    // some basic sanity checks
    if(isset($_GET['id'])) {
        //connect to the db
        $link = mysql_connect('localhost', 'root', '') or die("Could not connect: " . mysql_error());

        // select our database
        mysql_select_db("poll") or die(mysql_error());

        // get the image from the db
        $sql = "SELECT image FROM polldetails WHERE image_name= $id";

        // the result of the query
        $result = mysql_query("$sql") or die("Invalid query: " . mysql_error());

        // set the header for the image
        header("Content-type: image/jpg");
        echo mysql_result($result, 0);

        // close the db link
        mysql_close($link);
    }
    else {
        echo 'Please use a real id number';
    }
?>

显示损坏的图像链接,并且在通过萤火虫检查元素时显示未找到 url。这两个文件都驻留在同一个目录中。我正在使用 xampp。

【问题讨论】:

  • 是否显示部分图片

标签: php html mysql image


【解决方案1】:

在 file.php 中没有设置 $id 变量:

$id = $_GET[];

而且你的sql也有错误:

$sql = "SELECT image FROM polldetails WHERE image_name= '$id'";

不要忘记 $id 周围的单引号。

顺便说一句:您的代码可用于 sql 注入。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-06
    • 2023-03-25
    • 1970-01-01
    • 2012-11-16
    相关资源
    最近更新 更多