编辑后无法更新名称并删除图片

时间:2015-01-19 11:58:40

标签: php

我有两个问题:

  1. 名称无法更新
  2. 即使我没有上传新的个人资料照片也会被删除(现有的)。我上传图片没有问题。
  3. this is where I should edit my profile.

    i DIDN'T upload a new image but the picture is deleted AFTER I clicked Save

    以下是代码

    if(isset($_POST['submit'])){
    
            $target_dir = "images/staff/";
            $target_dir = $target_dir . basename($_FILES["new_profilepicture"]["name"]);
            $uploadOk=1;
    
            if (file_exists($target_dir . $_FILES["new_profilepicture"]["name"])) {
            echo "Sorry, file already exists.";
            $uploadOk = 0;
        }
            if ($uploadOk==0) {
            echo "Sorry, your file was not uploaded.";
        } 
        else { 
        if (move_uploaded_file($_FILES["new_profilepicture"]["tmp_name"], $target_dir)) {
            $imageup = $target_dir;
            echo "<img src='" . $imageup . "' />";
        } else {
            echo "Sorry, there was an error uploading your file.";
        }
    }
            $_var1 = $_POST['new_name'];
            $_var2 = $_POST['new_email'];
            $_var3 = $_POST['new_password'];
            $_var4 = $_POST['new_contactno'];
            $_var5 = $_POST['new_icno'];
            $_var6 = $_POST['new_address'];
            $_var7 = $_POST['new_status'];
            $_var8 = $imageup;
    
            $query1 =   "UPDATE staff
                                SET StaffName='$_var1', StaffEmail='$_var2', StaffPassword='$_var3', StaffContactNo='$_var4', StaffICNo='$_var5', StaffAddress='$_var6', StaffStatus='$_var7', StaffProfilePicture='$_var8'
                                WHERE StaffID='$staffID'";
    
            $success = mysql_query($query1);//is mysql query working?
    
                if($success){
                    $oldprofilepicture = $staff['StaffProfilePicture'];
                    if(file_exists($oldprofilepicture)){
                        unlink($oldprofilepicture);//delete now
                    }
                header('location:staff_profile.php');
                }
            }
    

    以下是输入新名称的行。

    <tr>
                    <td width="170">Full Name:</td>
                    <td><input type="text" name="new_name" size="30" value="<?php echo $staffname ?>" /></td>
                </tr>
    

1 个答案:

答案 0 :(得分:0)

关于图像,创建一个隐藏的输入字段,其值与数据库中的值相同,例如:

<input type="hidden" value="/path/to/img.ext" name="old-image" />

在此之前:

if (move_uploaded_file($_FILES["new_profilepicture"]["tmp_name"], $target_dir)) {

添加:

$imageup = $_POST['old-image'];

当然,您需要对$_POST数据进行验证。我把它从上面的例子中删除了。

这会将图像的默认值设置为已保存的版本。如果您上传新图片,那么将使用名称路径详细信息覆盖$imageup:)

关于名称没有变化,我看不出$staffId的值在哪里?