2016-02-15 121 views
-2

我一直在嘗試使用簡單的insertform來顯示上傳到數據庫的圖像。我只能將圖像名稱檢索到「數據文件」中,我想知道的是如何輕鬆地將圖像顯示在相同的表單位置而不是圖像名稱?使用php顯示來自MySQL數據庫的圖像

enter image description here

這是我的結果在這一刻,你可以看到,我只增加了圖像的插件之一,我願與改變「footer.jpg名」圖像顯示在這裏。

<html> 
<head> 
</head> 
<body> 
<form action="insertform.php" method="post"> 
Topic: <input type="text" name="topic"><br /> 
Name: <input type="text" name="name"><br /> 
Attendance: <input type="text" name="attendance"><br /> 
Image: <input type="file" name="image"><br /> 
<input type="submit" name="submit"> 
</form> 


<?php 
if (isset($_POST['submit'])){ 

$con = mysql_connect("localhost","username","password"); 

if (!$con){ 
    die("Can not connect: " . mysql_error()); 
} 

mysql_select_db("testingisfun",$con); 

$sql = "INSERT INTO Lectures (Topic,Name,Attendance,Image) VALUES ('$_POST[topic]', '$_POST[name]', '$_POST[attendance]', '$_POST[image]')"; 

mysql_query($sql,$con); 

mysql_close($con); 
} 
?> 

</body> 
</html> 

繼承人「數據文件」。

<html> 
<head> 
</head> 
<body> 
<?php 
$con = mysql_connect("localhost","username","password"); 
if (!$con){ 
    die("Can not connect: " . mysql_error()); 
} 
mysql_select_db("testingisfun",$con); 
$sql = "SELECT * FROM lectures"; 
$myData = mysql_query($sql,$con); 
echo "<table border = 1> 
<tr> 
<th>Topic</th> 
<th>Name</th> 
<th>Attendance</th> 
<th>Image</th> 
</tr>"; 
while($record = mysql_fetch_array($myData)){ 
    echo "<tr>"; 
    echo "<td>" . $record['Topic'] . "</td>"; 
    echo "<td>" . $record['Name'] . "</td>"; 
    echo "<td>" . $record['Attendance'] . "</td>"; 
    echo "<td>" . $record['Image'] . "</td>"; 
    echo "</tr>"; 
} 
echo "</table>"; 

mysql_close($con); 

?> 

</body> 
</html> 

感謝您的任何反饋!

+1

請[停止使用'mysql_ *'函數](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)。 [這些擴展](http://php.net/manual/en/migration70.removed-exts-sapis.php)已在PHP 7中刪除。瞭解[編寫]​​(http://en.wikipedia.org/ wiki/Prepared_statement)語句[PDO](http://php.net/manual/en/pdo.prepared-statements.php)和[MySQLi](http://php.net/manual/en/mysqli.quickstart .prepared-statements.php)並考慮使用PDO,[這真的很簡單](http://jayblanchard.net/demystifying_php_pdo.html)。 –

+1

[您的腳本存在SQL注入攻擊風險。](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) –

+2

http://stackoverflow.com/questions/7793009/how-to-retrieve-images-from-mysql-database-and -in-an-html-tag – Shad

回答

1

試試這個

echo "<td><img src='/{$record['Image']}'>" . $record['Image'] . "</td>"; 
+0

我還沒有分配任何目錄來存儲我的照片,我只是將它們發送到我的db中的表講座。我怎麼能從那裏檢索它? –

+0

我編輯我的文章... – paranoid

+0

這是我嘗試的結果。正如你所看到的,我只在三個插入的其中一個上添加了一個圖像,但我仍然只能顯示圖像名稱。 [鏈接](http://bildr.no/image/alFMeUsy.jpeg) –

0

我從我已經把在評論鏈接回答這個問題的答案。還有其他的替代方案也提供了這個問題。

echo "<td>" . $record['Image'] . "</td>"; 
    replace with 
    echo "<td><img src=\"data:image/jpeg;base64,"" . base64_encode($record['Image']) . "/></td>"; 

您存儲DB圖像和擷取這是給你的圖像數據是生疼顯示從數據就可以使用這種方法的圖像。 這不會幫助緩存,但它會解決問題。

+0

通過增加給定的線,我得到以下語法錯誤: 意外'(T_ENCAPSED_AND_WHITESPACE),預計標識符(T_STRING)或變量(T_VARIABLE)或數字(T_NUM_STRING) –

+0

哎呀一個閉引號失蹤只需要編輯它 – Shad

+0

現在,我得到結果如下: [link](http://bildr.no/image/YU84Skgr.jpeg) 正如你所看到的,它現在試圖在所有插入中添加相同的圖像,這不是我想要做的。 –

相關問題