2014-10-30 40 views
0

检索时显示空白我有四个文件:图片从数据库PHP的MySQL

  1. main.php我的html提交表单,它提交的图像和文本与图像

  2. storeinfo.php它将我的所有数据从html表单传送到数据库中,我的图像和文本已成功提交

  3. image.php从数据库中提取图像,并具有头部函数以将aimagetype转换为任何格式图像是png,jpeg等。

  4. show.php获取所有与图像一起发布的文本,并显示所有带有文本的图像但是图像不会显示,而是在图像无法显示时收到空白框。

我找不到我的错误,我猜它是与头功能image.php或当我试图显示与节目的HTML img标签的图像。 PHP。将图像(以blob存储)上载到数据库是成功的。 为什么不显示图像?

为了代码的每一页:

  1. main.php HTML表单

    <form enctype="multipart/form-data" action="storeinfo.php" method="POST"> 
    
    <table border=0 align=center bgcolor=black width=100%> 
    <tr><td colspan=2><h2>&nbsp</h2></td></tr> 
    </table> 
    
    
    <table border=0 align=center bgcolor=grey> 
    <tr><td colspan=2><h2>Animal Information</h2></td></tr> 
    <tr> 
    <td>Name</td><td><input type=text name="aname"></td> 
    </tr> 
    <tr> 
    <td>Description</td><td><input type=text name="adetails"></td> 
    </tr> 
    <tr> 
    <td>Photo</td><td><input type=file name="aphoto"></td> 
    </tr> 
    <tr> 
    <td></td><td><input type=submit name="submit" value="Store Information"></td> 
    </tr> 
    </table> 
    </form> 
    
  2. storeinfo.php

    <?php 
    $conn = mysql_connect("localhost","root",""); 
    if(!$conn) 
    { 
    echo mysql_error(); 
    } 
    $db = mysql_select_db("imagestore",$conn); 
    if(!$db) 
    { 
    echo mysql_error(); 
    } 
    $aname = $_POST['aname']; 
    $adetails = $_POST['adetails']; 
    $aphoto = addslashes (file_get_contents($_FILES['aphoto']['tmp_name'])); 
    $image = getimagesize($_FILES['aphoto']['tmp_name']);//to know about image type etc 
    
    $imgtype = $image['mime']; 
    
    $q ="INSERT INTO animaldata VALUES('','$aname','$adetails','$aphoto','$imgtype')"; 
    
    $r = mysql_query($q,$conn); 
    if($r) 
    { 
    echo "Information stored successfully"; 
    } 
    else 
    { 
    echo mysql_error(); 
    } 
    ?> 
    
  3. image.php

    <?php 
    
    $conn = mysql_connect("localhost","root",""); 
    if(!$conn) 
    { 
    echo mysql_error(); 
    } 
    $db = mysql_select_db("imagestore",$conn); 
    if(!$db) 
    { 
    echo mysql_error(); 
    } 
    $id = $_GET['id']; 
    $q = "SELECT aphoto,aphototype FROM animaldata where id='$id'"; 
    $r = mysql_query("$q",$conn); 
    if($r) 
    { 
    
    $row = mysql_fetch_array($r); 
    $type = "Content-type: ".$row['aphototype']; 
    header($type); 
    echo $row['aphoto']; 
    } 
    else 
    { 
    echo mysql_error(); 
    } 
    
    ?> 
    
  4. show.php

    <?php 
    //show information 
    
    
    $conn = mysql_connect("localhost","root",""); 
    if(!$conn) 
    { 
    echo mysql_error(); 
    } 
    $db = mysql_select_db("imagestore",$conn); 
    if(!$db) 
    { 
    echo mysql_error(); 
    } 
    
    $q = "SELECT * FROM animaldata"; 
    $r = mysql_query("$q",$conn); 
    if($r) 
    { 
    while($row=mysql_fetch_array($r)) 
    { 
    //header("Content-type: text/html"); 
    echo "</br>"; 
    echo $row['aname']; 
    echo "</br>"; 
    echo $row['adetails']; 
    echo "</br>"; 
    
    //$type = "Content-type: ".$row['aphototype']; 
    //header($type); 
    
    //$lastid = mysql_insert_id(); 
    // $lastid = $lastid; 
    //echo "Your image:<br /><img src=image.php?id=$lastid />"; 
    
    echo "<img src=image.php?id=".$row['id']." width=300 height=100/>"; 
    
    
    } 
    } 
    else 
    { 
    echo mysql_error(); 
    } 
    
    
    ?> 
    
+0

什么类型是您使用存储在数据库内容image.php?此外,我不太确定您是否可以将二进制内容保存到数据库中。 – Tomasz 2014-10-30 21:26:37

+1

要开始你应该引用你的图像源字符串:'src ='image.php?id =“。$ row ['id']。”''。除此之外,您应该尝试缩小问题范围,您在开发人员工具的网络选项卡,mysql错误消息或图像内容中获取图像的反应是什么? – jeroen 2014-10-30 21:26:51

+0

我使用的ID,aname varchar 200,adetails文本,BLOB for aimage,aphototype varchar 200 – Tom 2014-10-30 21:31:04

回答

0

所有我发现了如何做到这一点,你正试图在这里做的东西教程第一:你 http://www.mysqltutorial.org/php-mysql-blob/

第二应该使用mysql_escape_string(file_get_contents($ _ FILES ['aphoto'] ['tmp_name']))而不是addshlashes。

根据这2条规则,你应该能够弄清楚你的代码有什么问题,你也可以尝试使用更小的图片。

0

您的代码有许多问题,但最值得注意的是您正在使用deprecated mysql functions,并且您的代码易受SQL injection attack影响。

我已将storeinfo.phpimage.php改写为使用mysqli扩展名,并使用参数绑定来缓解SQL注入。我会留下重写show.php作为练习。

请注意,我对表的结构做了一些假设,因此您可能需要对SQL代码进行一些调整。

storeinfo.php

$aname = $_POST['aname']; 
$adetails = $_POST['adetails']; 
$aphoto = file_get_contents($_FILES['aphoto']['tmp_name']); 
$image = getimagesize($_FILES['aphoto']['tmp_name']);//to know about image type etc 
$imgtype = $image['mime']; 

$conn = new mysqli("localhost","root","", "imagestore"); 
if ($conn->connect_errno) { 
    echo "Failed to connect to MySQL: (" . $conn->connect_errno . ") " . $conn->connect_error; 
} 

if (!($stmt = $conn->prepare("INSERT INTO animaldata (aname, adetails, aphoto, aphototype) VALUES(?, ?, ?, ?)"))) { 
    echo "Prepare failed: (" . $conn->errno . ") " . $conn->error; 
} 
if (!$stmt->bind_param("ssbs", $aname, $adetails, $aphoto, $imgtype)) { 
    echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error; 
} 
$stmt->send_long_data(2, $aphoto); 

if (!$stmt->execute()) { 
    echo "Insert failed: (" . $conn->errno . ") " . $conn->error; 
} else { 
    echo "Information stored successfully"; 
} 

$conn = new mysqli("localhost","root","", "imagestore"); 
if ($conn->connect_errno) { 
    echo "Failed to connect to MySQL: (" . $conn->connect_errno . ") " . $conn->connect_error; 
} 

if (!($stmt = $conn->prepare("SELECT aphoto, aphototype FROM animaldata where id=?"))) { 
    echo "Prepare failed: (" . $conn->errno . ") " . $conn->error; 
} 
if (!$stmt->bind_param("i", $_GET['id'])) { 
    echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error; 
} 

if (!$stmt->execute()) { 
    echo "Select failed: (" . $conn->errno . ") " . $conn->error; 
} else { 
    $stmt->bind_result($aphoto, $aphototype); 
    $stmt->fetch(); 

    header("Content-type: ".$aphototype); 
    echo $aphoto; 
}