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

时间:2017-06-30 16:32:04

标签: php

我可以在更新页面中输入所有类型值视图但是,我无法显示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>

2 个答案:

答案 0 :(得分:0)

您可以像这样修改您的更新代码:

如果没有选择图像它将不会更新数据库中的图像它只会在选择图像时更新数据库中的图像

<?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>";
}
?>

答案 1 :(得分:-1)

<?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");
    
}

?>