【问题标题】:Display image from database in a php table在 php 表中显示数据库中的图像
【发布时间】:2012-10-12 09:36:14
【问题描述】:

我有一个简单的表格,有人填写,然后另一个表格运行查询以检索数据。

一直在考虑在表单中包含上传的图像,然后让表格显示包含此图像的表单数据。

这是我的一些代码

 if ($_SERVER['REQUEST_METHOD'] == 'POST'){
    $uname = $_POST['username'];
    $password = $_POST['password'];

    $uname = htmlspecialchars($uname);
    $password = htmlspecialchars($password);

    //==========================================
    //  CONNECT TO THE LOCAL DATABASE
    //==========================================
    $user_name = "xxxxxx";
    $pass_word = "xxxxxx";
    $database = "xxxxx";
    $server = "xxxxxx";

    $db_handle = mysql_connect($server, $user_name, $pass_word);
    $db_found = mysql_select_db($database, $db_handle);

    if ($db_found) {



        $SQL = "SELECT * FROM students WHERE L1 = '$uname' AND L2 = '" .md5 ($_POST['password'])."'";
        $result = mysql_query($SQL);
        $num_rows = mysql_num_rows($result);

    //====================================================
    //  CHECK TO SEE IF THE $result VARIABLE IS TRUE
    //====================================================

        if ($result) {
            if ($num_rows > 0) {
                $color="1";


$result = mysql_query("SELECT * FROM entry, students   WHERE entry.studentName = students.studentName AND students.L1='$uname' ") 
or die(mysql_error());  

echo "<p>You records as of ";
echo date('l jS \of F Y h:i:s A');
echo "<table border='1' cellpadding='2' cellspacing='0'>";
echo "<tr> <th>Date</th><th>Student Name</th> <th>Tutor name</th> <th>Procedure name</th> <th>Grade</th><th>Student Reflection</th><th>Tutor Comments</th><th>Professionalism</th> <th>Communication</th> <th>Alert</th> <th>Dispute</th><th>Username</th> <th>Image</th></tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {

if($color==1){
echo "<tr bgcolor= >
<td>".$row['date']."</td><td>".$row['studentName']."</td><td>".$row['tutorName']."</td><td>".$row['procedureName']."</td><td>".$row['grade']."</td><td>".$row['studentReflection']."</td><td>".$row['tutorComments']."</td><td>".$row['professionalism']."</td><td>".$row['communication']."</td><td>".$row['alert']."</td><td>".$row['dispute']."<td>".$row['L1']."</td></td>
<td><img src='images/".$row['studentImage']."'></td>;


</tr>";

// Set $color==2, for switching to other color
$color="2";
}

// When $color not equal 1, use this table row color
else {
echo "<tr bgcolor='#4eb557'>
<td>".$row['date']."</td><td>".$row['studentName']."</td><td>".$row['tutorName']."</td><td>".$row['procedureName']."</td><td>".$row['grade']."</td><td>".$row['studentReflection']."</td><td>".$row['tutorComments']."</td><td>".$row['professionalism']."</td><td>".$row['communication']."</td><td>".$row['alert']."</td><td>".$row['dispute']."<td>".$row['L1']."</td></td>
<td><img src='images/".$row['studentImage']."'></td>;
</tr>";
// Set $color back to 1
$color="1";
}

}
echo '</table>';

当结果回来时,我已经获取了除了图像之外的所有数据,图像只是显示为大量字符。

我哪里错了?

【问题讨论】:

  • 您应该将图像作为文件上传并将其路径存储在数据库中。
  • 您是将图像数据存储在数据库中还是实际图像的路径?
  • 我已经尝试了这两种方法,但似乎都不起作用。因此,我将尝试通过将图像存储在数据库中来使其工作
  • 永远不要将图像直接存储在数据库中。始终将它们存储在一个文件夹中,并将图像的路径存储在数据库中。完成后,检查“studentImage”字段中存储的内容。然后将该链接放入浏览器,看看您是否看到图像。让我们知道会发生什么。

标签: php mysql


【解决方案1】:

你的表格有这个吗?

enctype=multipart/form-data

在里面吗?否则,您将无法将图像作为文件上传。
确保您的表单至少看起来像这样:

<form method="post" action="URL_THINGY" enctype=multipart/form-data>
     (The rest of your form here)
<form>

【讨论】:

    【解决方案2】:

    如果你将图像内容存储在数据库中,然后用 PHP 显示,你应该做两件事:

    1. 在将其存储到数据库之前进行 base64 编码,这样数据就不会包含奇怪/错误的字符
    2. 显示图片时,在你的php中回显它们并将文件头设置为图片:
    header('Content-Type: image/png');

    但正如一些人已经说过的,最好为您的图片随机命名generate,将其存储在一个文件夹中,然后将图片的路径存储在您的数据库中。

    【讨论】:

      【解决方案3】:

      由于您需要将图像存储在数据库中,这里有一个 SO link 给遇到同样问题的人。

      【讨论】:

      • 我不需要将它们存储在数据库中,但只是认为这将是更简单的选择。
      • 我的意思是,既然您已经在存储它们,那么该链接将向您展示如何正确地从数据库中检索它们并显示它们。更好的解决方案是将它们存储在文件系统中,并将路径存储在数据库中。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-01
      • 2014-12-03
      • 2016-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-07
      相关资源
      最近更新 更多