图像未显示(图像损坏)但搜索有效

时间:2013-11-18 06:05:46

标签: php mysql ajax image searchable

我是编程方面的新手,并且一直致力于一个可搜索的数据库,可以通过输入关键字来检索图像,按下提交后会显示结果和图片。

但到目前为止,我没有运气让图片显示(链接/图像损坏),但我的搜索表格确实有效,并且能够正确检索名称或结果。

我在phpmyadmin名称中的表是,我有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();
}

&GT;

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!';

    }

} 

&GT;

注册新信息的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">

3 个答案:

答案 0 :(得分:0)

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

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/>";

答案 1 :(得分:0)

根据我的经验,当我尝试从数据库显示图像时,图像损坏的问题是图像的长度,这意味着从数据库中放入varchar的长度时,应将其更改为长文本。

答案 2 :(得分:-1)

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

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

例如:

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