【发布时间】: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 当然是的 :) 我计划在本周末处理已经完成的代码。谢谢