2014-10-30 78 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; 
}