问题描述
我开发了一个用户个人资料,他可以在首次登录时上传他的照片。他的个人资料图片保存到文件夹,图像路径保存在数据库中。现在我想使用他的user_name获取该图像,我将其设置为唯一。当用户通过隐藏的提交按钮保存他的个人资料图片时,我将他的user_name保存在数据库中。
但这个获取的图像代码不起作用
我的尝试:
这是我的完全欢迎。用户日志更改他的个人资料图片并查看他的个人资料详细信息的php文件。
I developed a user profile where he can upload his picture at his first login. His profile picture saves to a folder and image path saves in the database. Now I want to fetch that image using his user_name, which I have make it unique. when user save his profile picture, through a hidden submit button, I save his user_name in the database.
But this fetching image code does not work
What I have tried:
This is my complete welcome. php file where user log change his profile picture and see his profile details.
<?php include('config.php');?>
<?php
// index.php
session_start();
if(!isset($_SESSION['username']))
{
header("Location: login.php");
exit();
}
$username = $_SESSION['username'];
$sql = "SELECT * FROM services WHERE user_name = '".$_SESSION['username']."'";
$result = $con->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
?>
<!-- Save username and profile picture-->
<link rel="stylesheet" type="text/css" href="css/welcome_php.css">
<?php include('header.php');?>
<div class="container">
<div class="row">
<div class="col-md-7 ">
<div class="panel panel-default">
<div class="panel-heading">
<a style="float: right" href="logout.php"> Logout</a>
<center> <h4 >User Profile</h4></center>
</div>
<div class="panel-body">
<div class="box box-info">
<div class="box-body">
<div class="col-sm-6">
<form method="post" action="ajaxupload.php" enctype="multipart/form-data">
<h5 style="color:#a66a6a;">Set your profile picture</h5>
<input type="file" name="Filename">
<input type="hidden" name="Description" value="<?php echo $row ['user_name']; ?>" />
<br/>
<input TYPE="submit" name="upload" value="Submit" class="btn1" />
</form>
</div>
<div class="col-sm-6">
<h4 style="color:#a66a6a;"><?php echo $row ['name']; ?></h4></span>
<span><p><?php echo $row ['service']; ?></p></span>
</div>
<div class="clearfix"></div>
<hr style="margin:5px 0 5px 0;">
<div class="col-sm-5 col-xs-6 tital " >Name</div><div class="col-sm-7 col-xs-6 "><?php echo $row ['name']; ?></div>
<div class="clearfix"></div>
<div class="bot-border"></div>
<div class="col-sm-5 col-xs-6 tital " >User Name</div><div class="col-sm-7"> <?php echo $row ['user_name']; ?></div>
<div class="clearfix"></div>
<div class="bot-border"></div>
<div class="col-sm-5 col-xs-6 tital " >Password</div><div class="col-sm-7"> <?php echo $row ['password']; ?></div>
<div class="clearfix"></div>
<div class="bot-border"></div>
<div class="col-sm-5 col-xs-6 tital " >email</div><div class="col-sm-7"><?php echo $row ['email']; ?></div>
<div class="clearfix"></div>
<div class="bot-border"></div>
<div class="col-sm-5 col-xs-6 tital " >District</div><div class="col-sm-7"><?php echo $row ['district']; ?></div>
<div class="clearfix"></div>
<div class="bot-border"></div>
<div class="col-sm-5 col-xs-6 tital " >City</div><div class="col-sm-7"><?php echo $row ['city']; ?></div>
<div class="clearfix"></div>
<div class="bot-border"></div>
<div class="col-sm-5 col-xs-6 tital " >Address</div><div class="col-sm-7"><?php echo $row ['address']; ?></div>
<div class="clearfix"></div>
<div class="bot-border"></div>
<div class="col-sm-5 col-xs-6 tital " >Service</div><div class="col-sm-7"><?php echo $row ['service']; ?></div>
<div class="clearfix"></div>
<div class="bot-border"></div>
<div class="col-sm-5 col-xs-6 tital " >Years</div><div class="col-sm-7"><?php echo $row ['years']; ?></div>
<div class="clearfix"></div>
<div class="bot-border"></div>
<div class="col-sm-5 col-xs-6 tital " >Details</div><div class="col-sm-7"><?php echo $row ['details']; ?></div>
<center><a href="update_userinfo.php" class="btn" role="button">Update</a></center>
<?php
}
}
?>
<?php
$sql="SELECT image_path FROM images where user_name = '".$_SESSION['username']."'";
$result = $con->query($sql);
$file_name=$row['file_name'];
$image_path=$row['image_path'];//here you get path where you store image for user1
?>
<img src=<?php echo"$image_path";?> width=500 height=400> <br>
这是一个ajax上传文件,它将图像保存在数据库中
This is ajax upload file which saves image in the ddatabse
<?php
$fileExistsFlag = 0;
$fileName = $_FILES['Filename']['name'];
$link = mysqli_connect("localhost","root","","construction") or die("Error ".mysqli_error($link));
/*
* Checking whether the file already exists in the destination folder
*/
$query = "SELECT file_name FROM images WHERE file_name='$fileName'";
$result = $link->query($query) or die("Error : ".mysqli_error($link));
while($row = mysqli_fetch_array($result)) {
if($row['filename'] == $fileName) {
$fileExistsFlag = 1;
}
}
/*
* If file is not present in the destination folder
*/
if($fileExistsFlag == 0) {
$target = "uploads/";
$fileTarget = $target.$fileName;
$tempFileName = $_FILES["Filename"]["tmp_name"];
$fileDescription = $_POST['Description'];
$result = move_uploaded_file($tempFileName,$fileTarget);
/*
* If file was successfully uploaded in the destination folder
*/
if($result) {
echo "Your file <html>".$fileName."</html> has been successfully uploaded";
$query = "INSERT INTO images(image_path,file_name,user_name) VALUES ('$fileTarget','$fileName','$fileDescription')";
$link->query($query) or die("Error : ".mysqli_error($link));
}
else {
echo "Sorry !!! There was an error in uploading your file";
}
mysqli_close($link);
}
/*
* If file is already present in the destination folder
*/
else {
echo "File <html>".$fileName."</html> already exists in your folder. Please rename the file and try again.";
mysqli_close($link);
}
?>
推荐答案
这篇关于如何在PHP中使用user_name获取用户的个人资料图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!