更新表单详细信息后获取空白图像

问题描述:

In this code when I navigate to the update form I get all the details as per database but when I update the form without selecting the image file it shows blank in the table.

if (isset($_POST['update_sub_categories']))
    {
    $file = $_FILES['Subcategory_image']['tmp_name'];

    this are the conditions for update images
    if (file_exists($file))
        {
        $errors = array();
        $maxsize = 2097152;
        $acceptable = array(
            'image/jpeg',
            'image/jpg',
            'image/gif',
            'image/png'
        );
        if (($_FILES['Subcategory_image']['size'] >= $maxsize) || ($_FILES["Subcategory_image"]["size"] == 0))
            {
            $errors[] = 'File too large. File must be less than 2 megabytes.';

            // code...

            }

        if (!in_array($_FILES['Subcategory_image']['type'], $acceptable) && (!empty($_FILES["Subcategory_image"]["type"])))
            {
            $errors[] = 'Invalid files type. Only JPG, GIF and PNG types are accepted';
            }
        }

Conditions end at this point if no errors are found, the file will be uploaded

    if (count($errors) === 0)
        {
        move_uploaded_file($_FILES['Subcategory_image']['tmp_name'], 'images/categories/' . $_FILES['Subcategory_image']['name']);
        $Subcategory_image = $_FILES['Subcategory_image']['name'];

        // code...

        $m->set_data('Category_id', $Category_id);
        $m->set_data('Subcategory_description', $Subcategory_description);
        $m->set_data('Subcategory_name', $Subcategory_name);
        $m->set_data('Subcategory_image', $Subcategory_image);
        $a = array(
            'Category_id' => $m->get_data('Category_id') ,
            'Subcategory_description' => $m->get_data('Subcategory_description') ,
            'Subcategory_name' => $m->get_data('Subcategory_name') ,
            'Subcategory_image' => $m->get_data('Subcategory_image') ,
        );
        $q = $d->update("sub_categories", $a, "Subcategory_id='$Subcategory_id'");
        if ($q > 0)
            {
            header("location:Manage_subcategories.php");
            }
          else
            {
            echo "Error";
            }
        }
      else
        {
        header("location:Manage_subcategories.php?msg=invalidfile");
        }
    }

So how can i solve this? When i click on the submit button without selecting any image files it shows blank at the table and it does not show the image that is already available

You have to put this condition

    $Subcategory_image ="";
        if($Subcategory_id){
           $sql = $d->select("sub_categories", "Subcategory_id="$Subcategory_id);
if($sql->num_rows>0){
        $data=mysqli_fetch_array($sql); 
        $Subcategory_image = $data["Subcategory_image"];
}
        }
            $check = getimagesize($_FILES["Subcategory_image"]["tmp_name"]);
            if($check!=false){
             move_uploaded_file($_FILES['Subcategory_image']['tmp_name'], 'images/categories/' . $_FILES['Subcategory_image']['name']);
             $Subcategory_image = $_FILES['Subcategory_image']['name'];
             $m->set_data('Subcategory_image', $Subcategory_image);
            }
             $m->set_data('Category_id', $Category_id);
             $m->set_data('Subcategory_description', $Subcategory_description);
             $m->set_data('Subcategory_name', $Subcategory_name);

also assign existing image to $Subcategory_image before this condition so that if file control is empty it get old image else of making old image field empty in db table

Yeah this is because the image input is taking as null as uploading it to database .. add a additional condition as when there is no image input assign the previous image input to the variable