Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 编辑后无法更新名称并删除图片_Php - Fatal编程技术网

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

Php 编辑后无法更新名称并删除图片,php,Php,我有两个问题: 名称无法更新 即使我没有上传一张新的个人资料图片,但该图片已被删除。我没有问题,但在上传图像。 下面是代码 if(isset($_POST['submit'])){ $target_dir = "images/staff/"; $target_dir = $target_dir . basename($_FILES["new_profilepicture"]["name"]); $uploadOk=1; if (

我有两个问题:

名称无法更新 即使我没有上传一张新的个人资料图片,但该图片已被删除。我没有问题,但在上传图像。 下面是代码

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>

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

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

当然,您需要对$u POST数据进行验证。我在上面的例子中没有提到它

这将图像的默认值设置为保存的版本。如果上载新图像,则将使用名称路径详细信息覆盖$imageup:

关于名称未更改,我看不到$staffId的值设置在哪里?

$oldprofilepicture=$staff['StaffProfilePicture'];这不是新的个人资料图片吗?您应该在更新之前删除图片。在更新之前使用select查询并使用oldprofilepicture。警告:mysql_uu函数在PHP 5.5.0中已弃用,将来将被删除。相反,应该使用or扩展名。我建议使用
if (move_uploaded_file($_FILES["new_profilepicture"]["tmp_name"], $target_dir)) {
$imageup = $_POST['old-image'];