0

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

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

我在 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">

4

3 回答 3

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/>";
于 2013-11-18T06:19:11.980 回答
0

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

于 2018-12-12T07:33:55.147 回答
-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' />";
于 2013-11-18T08:18:59.087 回答