简体   繁体   English

PHP MySQLi 如何在 html 表中显示用户个人资料图像?

[英]PHP MySQLi How to show user profile image in html table?

I'm trying to show the profile image of the user in HTML table where i list the rest of user's info.我正在尝试在 HTML 表中显示用户的个人资料图片,我在其中列出了用户的其余信息。 But i can't get it to show, instead i'm getting Broken Image in the table.但是我无法显示它,而是在表格中显示了Broken Image And if i right click on the "broken image" and choose "Open image in new tab" i can see the path of the image and the link to the image Parent Directory I'm uploading the the path of the image to the database and storing the image to a folder ( images/profilepic ).如果我右键单击“损坏的图像”并选择“在新选项卡中打开图像”我可以看到图像的路径和图像父目录的链接我正在将图像的路径上传到数据库和将图像存储到文件夹 ( images/profilepic )。

Can someone help me please?有人能帮助我吗? Thanks a lot!非常感谢!

This is my insert.php code:这是我的 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);

?>

And this is where i'm showing it:这就是我展示它的地方:

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

Remove the single quote before the variable in your <img src :删除<img src变量的单引号:

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

The way you have it now, your variable is outside the src attribute:你现在拥有它的方式,你的变量在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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM