旧图片缺失

时间:2015-01-30 23:48:50

标签: php

所以我有"员工"数据库中的表,即StaffName,StaffAddress和StaffProfilePicture等。更新在名称,地址但不是图片上工作正常。数据库中似乎缺少旧图片,尽管我没有上传新图片。

    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 =   $mysqli->query("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
            echo "success";
            header('location:staff_profile.php');
            die;
            }else{
            echo "failed";
            }
        }

以下是图片的HTML表单

<tr>
    <td width="170">Old Profile Picture:</td>
    <td><img src="<?php echo $profilepicture ?>" width="100" height="80" /><br><br>
        <input type="file" name="new_profilepicture" />
</tr>

如何保留原有/旧图片?

1 个答案:

答案 0 :(得分:0)

在您的查询中,您有:

StaffProfilePicture = '$ _ var8'

所以它仍然更新数据库,因为$ imageup是空的/未定义的,所以是$ _var8,它将使用空值更新数据库。

所以添加一个if条件:

$_var8 = $imageup;

if($_var8 != '') {
    $query1 =   $mysqli->query("UPDATE staff SET StaffName='$_var1', StaffEmail='$_var2', StaffPassword='$_var3', StaffContactNo='$_var4', StaffICNo='$_var5', StaffAddress='$_var6', StaffStatus='$_var7', StaffProfilePicture='$_var8' WHERE StaffID='$staffID'");
} else {
    $query1 =   $mysqli->query("UPDATE staff SET StaffName='$_var1', StaffEmail='$_var2', StaffPassword='$_var3', StaffContactNo='$_var4', StaffICNo='$_var5', StaffAddress='$_var6', StaffStatus='$_var7' WHERE StaffID='$staffID'");
}

或者你可以通过其他方式做到这一点,但那就是你的问题是你丢失旧图像。希望它有所帮助。

干杯。