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

时间:2018-03-21 06:07:22

标签: php

在此代码中,当我导航到更新表单时,我按照数据库获取所有详细信息,但是当我更新表单而不选择图像文件时,它在表格中显示为空白。

The time is 8:05 -> it will round up into 8:00
The time is 8:23 -> it will round up into 8:30

条件在此时结束 如果没有找到错误,将上传文件

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';
            }
        }

那我怎么解决这个问题呢? 当我单击提交按钮而不选择任何图像文件时,它在表格中显示为空白,并且它不显示已经可用的图像

2 个答案:

答案 0 :(得分:0)

是的,这是因为图像输入与将其上传到数据库一样为空。添加一个附加条件,因为没有图像输入时将前一个图像输入分配给变量

答案 1 :(得分:0)

你必须把这个条件

    $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);

在此条件之前还将现有图像分配给$ Subcategory_image,这样如果文件控件为空,则会在db表中显示旧图像字段为空的旧图像