如果没有进行编辑,如何保留相同的配置图像



我已经制作了一个相当简单的配置文件上传功能。

您选择图像,然后提交,并且有效。但是,我现在想为用户提供更改配置文件映像的选项。我已经为用户键入更改配置文件设置的按钮而制作了此表格,它允许用户更改其个人资料名称或他的个人资料映像:

<form class="form-horizontal"  action="imageinsert.php" method="post" enctype="multipart/form-data">
   <div class="form-group">
     <label class="control-label col-sm-2" for="username" >NAME:</label>
     <div class="col-sm-7">
       <input type="text" class="form-control" id="username" placeholder="<?php echo $_SESSION["username"]; ?>" value="<?php echo $_SESSION["username"]; ?>" name="username"> 
     </div>
   </div>
   <div class="form-group">
     <label class="control-label col-sm-2" for="pwd">IMAGE:</label>
     <div class="col-sm-5">
       <p class ="formspacehack"></p>
       <input type="file" id="file" name="file"/>
     </div>
   </div>
   <div class="form-group">
     <div class="col-sm-offset-2 col-sm-10">
     </div>
   </div>
   <div class="form-group">
     <div class="col-sm-offset-2 col-sm-10">
       <button type="submit" class="btn btn-default">SAVE</button>
     </div>
   </div>
 </form>

我的问题是,如果用户打开此表格并更改他的名称,但没有选择任何图像,则它将上传非现有映像或换句话说:删除当前图像,因为它上传了null。

我认为我可以为我的动作页面做某种技巧吗?我已经在动作页面上制作了,因此配置文件图像是默认图像,如果图像为空。

   $file = $_FILES['file'];
$name = $file['name'];
if(empty($name)){
  $name = "optionimage.png";
};
$path = "./images/" . basename($name);

但是,如果用户已经拥有图像并打开编辑表单,但决定不对我来说,默认图像不是我的解决方案。然后,我希望用户保持与打开表格之前的相同图像。

编辑(我上传到DB代码):

$sqltwo = "UPDATE `users` SET `image` = '$path' WHERE `users`.`id` = '$id'";
if(mysqli_query($link, $sqltwo)){
    echo "Image uploaded successfully.";
    $newURL = "profile.php";

    header("Refresh:2; url=profile.php");
}  else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

您只需要检查是否添加图像如果不是,您不更新该字段,因此您必须将字段Quires分开。

if( isset($_POST['file']) and $_POST['file']){
   $file = $_FILES['file'];
}

最新更新