【问题标题】:Image not showing (broken image) but search works图像未显示(损坏的图像)但搜索有效
【发布时间】:2013-11-18 06:05:45
【问题描述】:

我是编程新手,一直在研究一个可搜索的数据库,该数据库可以通过输入关键字检索图像,按下提交后将显示结果和图片。

但到目前为止,我无法让图片显示(损坏的链接/图像),但我的搜索表单确实可以正常工作并且可以正确检索名称或结果。

我在 phpmyadmin 名称中的表是 shoes ,我有 3 列,1 个 id (int15 PRI) ,2 个品牌/型号 (varchar 50),3 个图片 (longblob)。

我的代码比较简单,希望你能帮帮我=)

文件名:search.php

<form action="search.php" method="POST">
Name: <input type ="text" name="search_name"> <input type="submit" value="Search">

<?php
if (isset($_POST['search_name'])) {
    $search_name = $_POST['search_name'];

    if (!empty($search_name)){

        if (strlen($search_name)>=3) {

        $query = "SELECT * FROM `shoes` WHERE `brand/model` LIKE '%".mysql_real_escape_string($search_name)."%'";
        $query_run = mysql_query($query);
        $query_num_rows = mysql_num_rows($query_run);

        if ($query_num_rows>=1) {
            echo $query_num_rows.' Results found:<br>';

            while ($query_row = mysql_fetch_array($query_run)) {


                    $picture = $query_row['picture'];
                    echo "</br>";
                    echo $query_row ['brand/model'];
                    echo "</br>";
                    echo "</br>";
                    //header("content-type: image/jpeg");
                    echo "<img src=image.php?id=".$row['id']." width=300 height=200/>";
                    echo "</br>";



            }
        } else {
            echo 'No Results Found.';
        }
    } else {
        echo 'Text field must be more than 3 characters.';
    }

} else {
    echo 'Text Field Cannot be Empty!';
}
}
?>

我这里有一个 image.php

<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
$conn = mysql_connect("localhost","root","");
if(!$conn){
    echo mysql_error();
}
$db = mysql_select_db("phsdatabase");

if(!$db){
echo mysql_error();
}

$id = $_GET['id'];
$query = "SELECT `picture` FROM shoes where id='$id'";
$query_run = mysql_query("$query",$conn);

if($query_run){
$row = mysql_fetch_array($query_run);
$type = "Content-type: image/jpeg";
header($type);
echo $row['picture'];
} else {
echo mysql_error();
}

?>

storeinfo.php 存储新信息,

<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
$conn = mysql_connect("localhost","root","");
if(!$conn)
{
echo mysql_error();
}
$db = mysql_select_db("phsdatabase",$conn);
if(!$db)
{
echo mysql_error();
}
@$brandmodel = $_POST['brand/model'];
@$picture = addslashes (file_get_contents($_FILES['picture']['tmp_name']));
@$image = getimagesize($_FILES['picture']['tmp_name']);//to know about image type etc

//$imgtype = $image['mime'];

if (isset($_POST['brand/model'])){
    $brandmodelentry = $_POST['brand/model'];

    if (!empty($brandmodelentry)){

        if (strlen($brandmodelentry)>=3) {
            $query ="INSERT INTO shoes VALUES('','$brandmodel','$picture')";
            $query_run = mysql_query($query,$conn);
            echo '<br>';
            echo "Information Stored Successfully!";

        } else {
            echo mysql_error();

        }
    echo '<br>';
    echo '<br>';
    echo "Thank you for Registering new information to our database!";
    } else{
        echo 'Text Field cannot be empty!';

    }

} 

?>

newentry.php 注册新信息

<form enctype="multipart/form-data" action="storeinfo.php" method="POST">


<center>Shoes Information</center>

Brand and Model Name<input type=text name="brand/model">

Picture of Shoes(Acceptable formats:<br>JPEG,JPG,PNG)<input type="file" name="picture" id ="picture">
<input type=submit name="submit" value="Store Information">

【问题讨论】:

  • 你的脚本 image.php 没有返回任何图片?或者你有什么问题?
  • 嗨,我的 image.php 只有在我输入 id 时才会返回图片。像 image.php?id=1。但是在我的 search.php 上,图片没有与我的搜索一起显示,它显示了一个断开的链接
  • $row['id'] ?不是$query_row['id']
  • omg 5-T,非常感谢!问题解决了!

标签: php mysql ajax image searchable


【解决方案1】:

你的代码是绝对正确的,除了单行,即

echo "<img src=image.php?id=".$row['id']." width=300 height=200/>";

您必须将行更改为:

echo '<img src="data:image/jpeg;base64,'
             .base64_encode($image['file_data']).'" width=300 height=200/>";

【讨论】:

  • 嗨 vineet,感谢您的及时回复.. 但是当我尝试访问我的 search.php 时遇到了一个问题,它给了我一个错误解析错误:语法错误,意外'否'(T_STRING ), 期待 ',' 或 ';'在第 58 行的 C:\xampp\htdocs\database\search.php 中,即 } } else { echo 'No Results Found.'; }
  • @user3003490 这里似乎是正确的,并认为这不是完整的代码
【解决方案2】:

根据我的经验,当我尝试从数据库显示图像时,我的图像被破坏的问题是图像的长度,我的意思是从数据库中你放置 varchar 的长度,你应该将其更改为长文本。

【讨论】:

    【解决方案3】:

    您的图片来源应该是图片文件扩展名而不是php扩展名,请检查:

    echo "<img src='any jpg,png or gif exetension path' width='300' height='200' />";
    

    例如:

    echo "<img src='imagename.png' width='300' height='200' />";
    

    【讨论】:

    • 嗨,我应该改变什么?我被困了 2 天
    • 实际上你的图片来源应该是任何图片扩展文件,比如jpg、png或gif,而不是php文件。
    • 您好,我有一个直接链接,例如 image.php?id=1 可以生成图像,但我想使用 id 而不是静态图像。
    • 你需要 iframe 来代替图片标签
    猜你喜欢
    • 2019-02-13
    • 2010-12-27
    • 2010-12-14
    • 2013-09-13
    • 1970-01-01
    • 2018-05-05
    • 2019-05-28
    • 1970-01-01
    • 2015-06-11
    相关资源
    最近更新 更多