如果在php中没有选择图像,如何更新旧图像

如果在php中没有选择图像,如何更新旧图像,php,Php,我可以在我的更新页面中查看所有输入类型值,但我不能显示ex:photo的文件输入值。在这里,我可以在一个单独的部分显示图像位置值 我的问题 当我更新唯一的姓名或母亲姓名或任何没有照片的人时,所有字段值都会正确更新,但我的图像会更新为空值。当我使用照片和其他详细信息更新时,只有我的照片更新正确。因此,请帮助我如何使用“编辑而不编辑”更新照片值 update.php <!doctype html> <html> <body> <?php error_re

我可以在我的更新页面中查看所有输入类型值,但我不能显示ex:photo的文件输入值。在这里,我可以在一个单独的部分显示图像位置值

我的问题

当我更新唯一的姓名或母亲姓名或任何没有照片的人时,所有字段值都会正确更新,但我的图像会更新为空值。当我使用照片和其他详细信息更新时,只有我的照片更新正确。因此,请帮助我如何使用“编辑而不编辑”更新照片值

update.php

<!doctype html>
 <html>
 <body>
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
$upd = $_GET['upd'];
mysql_connect('localhost','root','');
mysql_select_db("display");
$slc = "SELECT * from photos WHERE firstname = '$upd'";
$run = mysql_query($slc);
while($row=mysql_fetch_array($run)){
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$location=$row['location'];
}
?>
<div class="update">
<table align="center" border="">
<tr>
<td colspan="4"><center><h1 style="color:red">Student Form Updation</h1>
</center></td></tr>
<form method="post" enctype="multipart/form-data" action="update.php?upd=<?
php echo $firstname; ?> "/>
<tr><td><label>First Name:</label></td><td>
<input type="text" name="firstname"  value="<?php echo $firstname ?> " />
</td>
<td><label>Last Name:</label></td><td>
<input type="text"name="lastname" value="<?php echo $lastname?>" /></td>
</tr>
<td><label>Select Photo</label></td>
<td> <input type="file" name="image" class="ed" id="location" value= "<?php 
echo $location?>"/> <?php echo '<img width="100px" height="100px" 
src="'.$location.'">'; ?>
</td>
</tr>
<tr><td></td><td><input type="submit" name="update" value="Update" 
id="button1" /></td>
<td><input type="submit" formaction="errview.php" value="View" id="button1" 
/></td><td></td></tr>
</form>
</div>
<?php   
if(isset($_POST['update']))
{       
$nid = $_GET['upd'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$file=$_FILES['image']['tmp_name'];     
$image=addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name= addslashes($_FILES['image']['name']);          
move_uploaded_file($_FILES["image"]["tmp_name"],"photos/" . $_FILES["image"]
["name"]);          
$location="photos/" . $_FILES["image"]["name"];         
$update = "UPDATE photos SET firstname='$firstname', lastname = 
'$lastname',location='$location' WHERE firstname='$nid'";       
$run = mysql_query($update);        
echo "<script>alert('Update SuccessFull!');location.href='update.php?
upd=$firstname'</script>";
}
?>
</body>
</html>

学生表格更新

您可以按如下方式修改更新代码:

如果未选择图像,则不会更新数据库中的图像。如果选择了图像,则只会更新数据库中的图像

<?php
if (isset($_POST['update'])) {
    $nid        = $_GET['upd'];
    $firstname  = $_POST['firstname'];
    $lastname   = $_POST['lastname'];
    $file       = $_FILES['image']['tmp_name'];
    $image      = addslashes(file_get_contents($_FILES['image']['tmp_name']));
    $image_name = addslashes($_FILES['image']['name']);
    move_uploaded_file($_FILES["image"]["tmp_name"], "photos/" . $_FILES["image"]["name"]);
    $location = "photos/" . $_FILES["image"]["name"];
    if ((!($_FILES['image']['name']))) /* If there Is No file Selected*/ {
        $update = "UPDATE photos SET firstname='$firstname', lastname = 
'$lastname' WHERE firstname='$nid'";
    } else /* If file is  Selected*/ {
        $update = "UPDATE photos SET firstname='$firstname', lastname = 
'$lastname',location='$location' WHERE firstname='$nid'";

    }
    $run = mysql_query($update);
    echo "<script>alert('Update SuccessFull!');location.href='update.php?
upd=$firstname'</script>";
}
?>


您应该使用check检查文件名是否为空,不要更新列,否则请使用new-oneHi更新。我是PHP初学者。我不知道如何应用if语句。这是一个简单的方法,你可以用这个方法
<?php

if(empty($image)){
    
      $product_img1=$row_edit['image'];
}
else{
     $temp_name1=$_FILES['product_img1']['tmp_name'];
     move_uploaded_file($temp_name1,"product_images/$product_img1");
    
}

?>