【问题标题】:PHP MySQLi How to show user profile image in html table?PHP MySQLi 如何在 html 表中显示用户个人资料图像?
【发布时间】:2018-12-26 23:46:06
【问题描述】:

我正在尝试在 HTML 表格中显示用户的个人资料图像,其中列出了用户的其余信息。但我无法让它显示,而是在表格中得到Broken Image。如果我右键单击“损坏的图像”并选择“在新选项卡中打开图像”,我可以看到图像的路径和图像的链接Parent Directory 我正在将图像的路径上传到数据库并将图像存储到文件夹( images/profilepic )。

有人可以帮帮我吗?非常感谢!

这是我的 insert.php 代码:

<?php
$server = "localhost";
$user = "root";
$pass = "";
$dbname = "employees";

// Create connection
$conn = mysqli_connect($server, $user, $pass, $dbname);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$profile_file = basename($_FILES['profileimg']['name']);
$profile_path = "images/profilepic/$profile_file";
$profile_file = mysqli_real_escape_string($conn, $profile_file);

move_uploaded_file($_FILES['profileimg']['tmp_name'], $profile_path);


$sql = "INSERT INTO addemployees (profileimg)
        VALUES ('$profile_file')";

if (mysqli_query($conn, $sql)) {
  header("location: employees.php");

} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

//Close the connection
mysqli_close($conn);

?>

这就是我展示它的地方:

<table id="dtBasicExample" class="table table-bordered table-hover text-center">
                             <thead>
                               <tr class="naslov">
                                   <th class="th-sm">ID:</th>
                                   <th class="th-sm">Profile Picture:</th>
                                   <th class="th-sm">First Name:</th>
                                   <th class="th-sm">Last Name:</th>
                                   <th class="th-sm">DOB:</th>
                                   <th class="th-sm">EMBG:</th>
                                   <th class="th-sm">Work Position:</th>
                                   <th class="th-sm">Address:</th>
                                   <th class="th-sm">Contract:</th>
                                   <th class="th-sm" colspan="2">Опции:</th>
                               </tr>
                            </thead>

                            <tbody id="myTable">

                          <?php

                              $conn = mysqli_connect("localhost", "root", "", "employees");

                            if (!$conn) {
                            die("Connection failed: " . mysqli_connect_error());
                            }

                              $sql = "SELECT * from addemployees";
                              $result = $conn-> query($sql);

                              if ($result-> num_rows > 0) {
                             while ($row = $result-> fetch_assoc()) {




                               echo "<tr>
                                      <td>".$row['id']."</td>
                                      <td><img src='images/profilepic/'". $row['profileimg'] ."' border=0 class='tile' id='profileimg'></td>
                                      <td>".$row['fname']."</td>
                                      <td>".$row['lname']."</td>
                                      <td>".$row['dob']."</td>
                                      <td>".$row['embg']."</td>
                                      <td>".$row['workposition']."</td>
                                      <td>".$row['address']."</td>
</tr>";


                          }

                            echo "</table>";
                          }
                            else {
                            echo "0 results";
                          }

                        $conn-> close();

                              ?>

                              </tbody>

【问题讨论】:

  • 启用错误报告。另外,什么是存储在行中的图像、文件名或 BLOB?
  • 我正在存储一个文件名
  • @miken32 我刚刚检查了路径,它看起来不错。请参阅ScreenShot。谢谢
  • 另外请注意,您不应该依赖 DOM 检查器来查看您的 HTML。而是查看源代码。
  • @FunkFortyNiner 当然是的 :) 我计划在本周末处理已经完成的代码。谢谢

标签: php html image mysqli


【解决方案1】:

删除&lt;img src中变量之前的单引号:

<td><img src='images/profilepic/". $row['profileimg'] ."'
                                ^-- remove the ' here   ^-- but leave this one

按照你现在的方式,你的变量在src 属性之外:

// if $row['profileimg'] is image.jpg:
<img src='images/profilepic/'". $row['profileimg'] ."' border=0>

// will become:
<img src='images/profilepic/'image.jpg' border=0>
//                          ^-- end of the src='' argument

【讨论】:

  • 感谢 rickdenhaan。这就是它应该的路径。但我仍然收到“损坏的图像”
  • @Blagojce 您问题中的显示代码是否仍然有效?在您发布的屏幕截图中,src 的末尾有一个额外的',它不应该存在。
  • rickdenhaan 大眼睛 :) 非常感谢,我没有发现。 ' 惹麻烦了 :) 再次感谢。
猜你喜欢
  • 2023-01-27
  • 2014-10-30
  • 2016-06-26
  • 1970-01-01
  • 2021-08-14
  • 1970-01-01
  • 1970-01-01
  • 2021-12-15
  • 2020-09-21
相关资源
最近更新 更多