prevent image from submission if field is empty

Notice: Undefined offset: 1 in … on line 44,
im getting this error when i submitted image field empty, if i submit uploading image form works fine no errors inserts data into each database(4 database) sucessfully

Im bulding form with lot field and different database in single form.

//database connection
ini_set('display_errors','on');
try {
    $host       = 'localhost';
    $dbname     = 'parent';
    $user       = 'root';
    $password   = '';

    $conn = new PDO("mysql:host=$host;dbname=$dbname",$user,$password);
    $conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    $e->getMessage();
    //die("Something went wrong please contact your adminstrator");
}
<form method="post" enctype="multipart/form-data">
   Input field-1
   Input field-2
   Input field-3
   ......
   ....
   .....
   Input field-10
   $insert = $conn->prepare("INSERT INTO parent (field-1,field-2,.......,field-10) VALUES (:field-1,......,field-10)");

Now inserting gallery where im getting error when gallery(image)field is submitted empty 



//checking if image field is empty, if empty do nothing dont insert blank data in gallery database
    //i have used separte database for gallery

if (!empty($_FILES['multipleFile']['name'])) {//line 44 where im getting error when submitted without image
            
      $multiplefile = $_FILES['multipleFile']['name'];

      foreach ($multiplefile as $name => $value) {
        
        $allowImg = array('png','jpeg','jpg',''); 

        $fileExnt = explode('.', $multiplefile[$name]);

        if (in_array($fileExnt[1], $allowImg)) {

            if ($_FILES['multipleFile']['size'][$name] > 0 && $_FILES['multipleFile']['error'][$name]== 0) {
              
              $fileTmp = $_FILES['multipleFile']['tmp_name'][$name];
                  
              $newFile =  rand(). '.'. $fileExnt[1];

              $target_dir = 'upload/'.$newFile; 

              if (move_uploaded_file($fileTmp, $target_dir)) {
                //3rd database, database to store multiple image
                  $gallery = $conn->prepare("INSERT INTO gallery (gpranet_id,image,caption) VALUES (:parent_id,:image,:caption)");
                  $gallery->execute([
                        ':parent_id'=>$lastenterid,
                        ':image'=>$newFile,
                        ':caption'=>$newFile,
                    ]);
              }
            }
        }
      }
    }

//Uploading multiple image
<div class="form-group">
  <input type="file" name="multipleFile[]" multiple>
</div>  

<div class="form-group">
   <input type="submit" class="btn btn-primary" name="publish" value="Submit">
</div>

</form>

how can i debug this error

Notice: Undefined offset: 1 in … on line 44

Which im getting submitting empty image field, if user doesn’t have/ requried to insert image i want it to empty and dont want send empty string to database.

(Solved) array is excepting at least one value but when i submit empty field it’s submitting nothing but array expect at least one value, thats the reason for the error

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.