2016-02-15 122 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) 正如你所看到的,它现在试图在所有插入中添加相同的图像,这不是我想要做的。 –

相关问题