繁体   English   中英

我如何在我的网站上显示用户数据库中的个人资料图片?

[英]how would i show a profile picture on my website from my database from the users?

这是我用于我的用户页面 atm 的代码,它只在数据库中显示他们的名字

用户列表.php

<?php
include "header.php";      
include "footer.php";
include "db_conn.php";

?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Home</title>
    <link rel="icon" type="image/x-icon" href="fotos/favicon.ico">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <link rel="stylesheet" href="style.css">
    <link rel="stylesheet" href="styletwo.css">
  <script src="java/currentime.js"></script>
</head>
<body>
    <h3 style="text-align: center;">Users on the website</h3>
    <div id="usernamelist" style="border: solid black 1px; width: 60px; height: 20px; margin-left: auto; margin-right: auto;">
    <?php if ($user) {
    $sql = "SELECT * FROM users;";
 
    foreach ($conn->query($sql) as $row) {
        print $row['username'] . '<br>';   
    }

    /*
    $resultCheck = mysqli_num_rows($result);

        if ($resultCheck > 0) {
            while ($row = mysqli_fetch_assoc($result)) {
                echo $row['username'] . "<br>";
            }
        }*/

        }else { 
            header("Location: login.php");
            exit;
           } ?>
           </div>
           </div>
</body>
</html>

这就是处理图片的原因

个人资料picture.php

 if (isset($_FILES['pp']['name']) AND !empty($_FILES['pp']['name'])) {
         
         
         $img_name = $_FILES['pp']['name'];
         $tmp_name = $_FILES['pp']['tmp_name'];
         $error = $_FILES['pp']['error'];
         
         if($error === 0){
            $img_ex = pathinfo($img_name, PATHINFO_EXTENSION);
            $img_ex_to_lc = strtolower($img_ex);

            $allowed_exs = array('jpg', 'jpeg', 'png');
            if(in_array($img_ex_to_lc, $allowed_exs)){
               $new_img_name = uniqid($uname, true).'.'.$img_ex_to_lc;
               $img_upload_path = '../upload/'.$new_img_name;
               move_uploaded_file($tmp_name, $img_upload_path);

这是当有人更新他们的个人资料或现在注册时在网站上显示个人资料图片的内容我想让它通过数据库显示在网站上在这里输入图片描述

由于您将用户个人资料图像保存在文件夹中,因此您还应该将其名称插入用户表的一列(例如profile_image),这样您就可以找到每个用户的图像。

# Inserting => After saving image in a folder
$query = "INSERT INTO $tableName (profile_image)
VALUES($new_img_name)";
$conn->query($query);

然后读取数据。

function getUserData($id){
    # Reading
    global $conn;
    $query = "SELECT * FROM $tableName WHERE user_id=$id";
    $result = mysqli_query($conn, $query);
    $info = mysqli_fetch_assoc($result);
    return $info;
}

你可以这样称呼这个 function:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <?php require('db_conn.php')?>
    <?php
    function getUserData($id){
        # Reading
        global $conn;
        $query = "SELECT * FROM $tableName WHERE user_id=$id";
        $result = mysqli_query($conn, $query);
        $info = mysqli_fetch_assoc($result);
        return $info;
    }

    ?>
</head>
<body>
<?php
$userInfo= getUserData($id); ?>
<img src='<?= "../upload/".$userInfo["profile_image"] ?>' alt="">
</body>
</html>

暂无
暂无

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

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