[英]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.