Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

NaderH's avatar

Submit multiple text inputs with a file upload input via one php form

I need to submit ( insert into one table ) multiple text inputs with a file upload input via one php form but the whole operation failes and I got an error ( failed to add ) and after checking the error I found that this happens because of an empty input ( the upload file input ) because of the feilds validation, actually that happens ( the empty field ) because of the error ( undefined index ( the input file upload value ).

So I need help to get rid of this to complete sending the data.

and here is the upload.php code and the html form.

<form enctype="multipart/form-data" name="add_book" action="upload.php" method="post">

       <input type="text" name="book_title" placeholder="Book Title .." required="required">

       <input type="file" name="file_up" accept="image/*" required="required"/>

       <input type="submit" name="add_book" value="Submit">

</form>
<?php

if (isset($_POST['add_book'])) {
    $req_fields = array('book_title','file_up');
          validate_fields($req_fields);
  if(empty($errors)){

    $book_title = (isset($_POST['book_title ']) ? $_POST['book_title '] : null);
    $file_up = (isset($_POST['file_up']) ? $_POST['file_up'] : null);

    $query = "SELECT `id` FROM `books` WHERE `book_title ` = '$book_title ' OR `file_up` = '$file_up' LIMIT 1";
    $result = $db->query($query);
    if ($result && $db->num_rows($result) > 0) {
    //failed because the entered one of the two values is already exists in the db
    echo "error already exists";
    return ('page.php');
    }

    // upload file
    if (!empty($_FILES['file_up'])) {
      if(move_uploaded_file($_FILES["file_up"]["tmp_name"], "files/" . $_FILES["file_up"]["name"]))
      $file_up = "files/" . $_FILES["file_up"]["name"];
      echo "successfully added";
      return ('page.php');
    }
    else
      $file_up='';
      echo "failed to add";
      return ('page.php');

    // insert data to db
    $query = "INSERT INTO `books` (`book_title`, `file_up`) VALUES ('$book_title', '$file_up')";

          if($db->query($query)) {
             echo "successfully added";
             return ('page.php');
          } else {
             echo "failed to add";
             return ('page.php');
                 }
  } 
            else {
            echo $errors;
            return ('page.php');
                 }
}
?>
0 likes
16 replies
Snapey's avatar

if you move the file, you then return without updating the database

    if (!empty($_FILES['file_up'])) {
      if(move_uploaded_file($_FILES["file_up"]["tmp_name"], "files/" . $_FILES["file_up"]["name"]))
      $file_up = "files/" . $_FILES["file_up"]["name"];
      echo "successfully added";
      return ('page.php');
NaderH's avatar

@Snapey ,Ok, just to make some things clear, I made the code shorter in that question instead of writing the whole code ( there was 8 text inputs), and "failed to add" is the first one which is under $file_in=''; that was the one that I see, I checked it by making it in a different text msg, and the reason to fail is because it sends empty field because it gives me an error undefined index in the validation functions in the functions.php file, so the whole operation stops but ( if I removed enctype="multipart/form-data" from the html form or the validation from the upload.php file ) it sends the data with an empty file field. that is a messed up code I know I am still a beginner, I need to keep the enctype="multipart/form-data" from the html form and the validation from the upload.php file to make sure that every thing is good and in a right way, so I am thinking that the error undefined index file_up is the one that if I resolved, it will send the data with the file and the whole oertation will be completed successfully. that's the error I got which stops the whole operation, Notice: Undefined index: file_in in functions.php on line 44, and that's the code

/*--------------------------------------------------------------*/
/* Function for Checking input fields not empty
/*--------------------------------------------------------------*/
function validate_fields($var){
  global $errors;
  foreach ($var as $field) {
    $val = remove_junk($_POST[$field]);   // line 44
    if(isset($val) && $val==''){
      $errors = $field ." can't be blank.";
      return $errors;
    }
  }
}
NaderH's avatar

@Snapey , So you said if I moved the file, I will return without updating the database, So how to insert the inputs data (all of the 8 fields) with the file input field and also sending that file to a folder in the project directory?

Snapey's avatar

@NaderH move the file, get the file name and add it to the array of data to write to the database.

write the data to the database and then return.

NaderH's avatar

@Snapey , I did that already and it only sends the file name to the database with the rest of the inputs data but not sending the file itself to the folder in the project directory I want.

NaderH's avatar

@Snapey , I fixed the issue but the problem now is that I want to use the validation for all fields but if I did that I got error that the file input is empty because of the undefined index file_up in the functions.php file then the whole process will stop and If I removed the file_up from the validation array $req_fields = array('book_title','file_up'); to be like that $req_fields = array('book_title'); then everything is ok, so how to force validate that file_up input without getting error that the it is empty because it is undefined index?

Snapey's avatar

@NaderH test if it empty first, and skip the validation for that field if so.

NaderH's avatar

@Snapey , I already skipped the validation for it. And it is working well without it.

NaderH's avatar

@BillBux , well , we can't call it a real solution, I had to skip the validation for the file input field, and this is something I didn't like to do, I wanted to submit all with the field validation for all inputs.

Please or to participate in this conversation.