在php表中显示数据库中的图像

时间:2012-10-22 14:38:15

标签: php mysql

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

一直在想着在表单中包含图片上传,然后让表格显示包含此图片的表单数据。

这是我的一些代码

 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>';

当结果返回时,我已经获取了除了显示为一组字符的图像之外的所有数据。

我哪里错了?

3 个答案:

答案 0 :(得分:0)

你的表格有这个吗?

enctype=multipart/form-data

在里面?如果不是,您将无法将图像上传为文件 确保您的表单至少看起来像这样:

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

答案 1 :(得分:0)

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

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

    但正如一些人已经说过的,最好generate为您的图片添加一个随机名称,将其存储在一个文件夹中并仅存储 数据库中图像的路径。< / p>

答案 2 :(得分:0)

由于您需要在数据库中存储图像,因此对于遇到相同问题的人来说,这是一个SO link