[ad_1]
我开发了一个用户个人资料,他可以在第一次登录时上传他的照片。 他的个人资料图片保存到文件夹中,图像路径保存在数据库中。 现在我想使用他的用户名来获取该图像,我已使其独一无二。 当用户保存他的个人资料图片时,通过隐藏的提交按钮,我将他的用户名保存在数据库中。
但是这个获取图像的代码不起作用
但是这个获取图像的代码不起作用
我尝试过的:
这是我完全的欢迎。 php 文件,用户在其中更改他的个人资料图片并查看他的个人资料详细信息。
<?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">class="fa fa-sign-out"> 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上传文件,将图像保存在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); } ?>
解决方案1
除了我关于不显示任何包含以下代码的评论之外 img_src
多变的。
HTML 属性值应该加引号(浏览器通常也接受不带引号的属性值,但它不是有效的 HTML),并且如果它们包含空格或保留字符,则必须加引号:
PHP
<?php echo '<img src="' . $image_path . '" width="500" height="400">'; ?>
[EDIT]
此代码似乎是复制并粘贴在一起的,并且不会按预期工作:
PHP
$sql="SELECT image_path FROM images where user_name = '".$_SESSION['username']."'"; // Executing the query and assigning to $result // OK so far but it misses checking for errors $result = $con->query($sql); // What is $row? // The query result has been stored in $result! // There is probably something missing like //$row = $result->fetch_row(); // Finally, the above SQL query returns only one field: image_path // So 'file_name' will never be present $file_name=$row['file_name']; $image_path=$row['image_path'];//here you get path where you store image for user1
如果上述问题已修复以从数据库返回路径(如果没有,则打印一些反馈),则该路径可用于 img
标签,前提是文件存在并且是相对于 Web 服务器文档根(以“https://www.codeproject.com/”开头)或相对于当前页面的路径。
[/EDIT]
解决方案2
非常感谢您的帮助@Jochen。 考虑到您的评论后,我编写了如下代码
我
<?php $sql="SELECT * FROM images where user_name = '".$_SESSION['username']."'"; $result = $con->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { ?> <?php echo '<img src="' . $row['image_path']. '" width="500" height="400">'; ?> <?php } } ?>
[ad_2]
コメント