title: Using Form to Upload PDF to Server and Data to SQL Database
body: <p>I am trying to use a modal form to upload and retrieve PDF files. The form includes a file upload and descriptive text (Vendor Name, Category, and Notes) which gets sent to MySQL database through AJAX/PHP. I end up with a table with last column including a button (which I will want a click to open the PDF related to the row of text. The issue I am having is I can only upload to the database or upload the file when submitting the form, unable to do both at the same time. Can anyone provide thought based on the below code and images?</p>
  <p>Also, I am uploading directly to the webserver.. is there a &quot;best practice&quot; when it comes to uploading file (i.e. different fileserver)?</p>
  <pre><code>&lt;!-- The Modal/Form --&gt;
  
     &lt;div class=&quot;modal-body&quot;&gt;
      &lt;form method=&quot;post&quot; action=&quot;upload-manager.php&quot; id=&quot;insert_form&quot; enctype=&quot;multipart/form-data&quot;&gt;
       &lt;label&gt;Vendor&lt;/label&gt;
       &lt;input type=&quot;text&quot; name=&quot;vendor&quot; id=&quot;vendor&quot; class=&quot;form-control&quot; /&gt;
       &lt;br /&gt;
       &lt;label&gt;Category&lt;/label&gt;
       &lt;input type=&quot;text&quot; name=&quot;category&quot; id=&quot;category&quot; class=&quot;form-control&quot; /&gt;
       &lt;br /&gt; 
       &lt;label&gt;Notes&lt;/label&gt;
       &lt;textarea name=&quot;notes&quot; id=&quot;notes&quot; class=&quot;form-control&quot;&gt;&lt;/textarea&gt;
       &lt;br /&gt;  
       &lt;label&gt;Date&lt;/label&gt;
       &lt;input type=&quot;date&quot; name=&quot;date&quot; id=&quot;date&quot; class=&quot;form-control&quot;&gt;&lt;/textarea&gt;
  &lt;script&gt;
      $('#date').val(new Date().toJSON().slice(0,10));
  &lt;/script&gt;
       &lt;br /&gt;  
       &lt;label&gt;Import File&lt;/label&gt;
       &lt;input type=&quot;file&quot; name=&quot;file&quot; id=&quot;fileSelect&quot; class=&quot;form-control-file&quot; /&gt;
       &lt;br /&gt;
       &lt;input type=&quot;submit&quot; name=&quot;insert&quot; id=&quot;insert&quot; value=&quot;Done&quot; class=&quot;btn btn-success&quot; /&gt;
  
      &lt;/form&gt;
     &lt;/div&gt;
     &lt;div class=&quot;modal-footer&quot;&gt;
      &lt;button type=&quot;button&quot; class=&quot;btn btn-default&quot; data-dismiss=&quot;modal&quot;&gt;Close&lt;/button&gt;
     &lt;/div&gt;
    &lt;/div&gt;
   &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;!-- PHP for uploading file to server --&gt;
  
  &lt;?php
  
  if(isset($_FILES['file'])) {
  
      $file = $_FILES['file'];
  
      //file details
  
      $name = $file['name'];
      $tmp_name = $file['tmp_name'];
  
  
      $tmp_file_name = &quot;{$name}&quot;;
      $tmp_file_path = &quot;files/{$tmp_file_name}&quot;;
      
  
      // mode the file
  
      move_uploaded_file($tmp_name, $tmp_file_path);
  
  }
  
  ?&gt;
  
  &lt;!-- Script for sending to SQL database --&gt;
  
  &lt;script&gt;  
  $(document).ready(function(){
   $('#insert_form').on(&quot;submit&quot;, function(event){  
    event.preventDefault();  
    if($('#vendor').val() == &quot;&quot;)  
    {  
     alert(&quot;Vendor is required&quot;);  
    }  
    else if($('#category').val() == '')  
    {  
     alert(&quot;Categoy is required&quot;);  
    }  
    else if($('#date').val() == '')
    {  
     alert(&quot;Date is required&quot;);  
    }
     
    else  
    {  
     $.ajax({  
      url:&quot;insert.php&quot;,  
      method:&quot;POST&quot;,  
      data:$('#insert_form').serialize(),  
      beforeSend:function(){  
       $('#insert').val(&quot;Inserting&quot;);  
      },  
      success:function(data){  
       $('#insert_form')[0].reset();  
       $('#add_data_Modal').modal('hide');  
       $('#dataTable').html(data);
       $('#insert').val(&quot;Done&quot;); 
       $('#date').val(new Date().toJSON().slice(0,10));   
      }  
     });  
    }  
   });
  </code></pre>
  <pre><code>insert.php
  
  &lt;?php
  //insert.php  
  include_once 'db.php';
  if(!empty($_POST))
  {
   $output = '';
      $vendor = mysqli_real_escape_string($connect, $_POST[&quot;vendor&quot;]);  
      $category = mysqli_real_escape_string($connect, $_POST[&quot;category&quot;]);  
      $notes = mysqli_real_escape_string($connect, $_POST[&quot;notes&quot;]);
      $date = mysqli_real_escape_string($connect, $_POST[&quot;date&quot;]);
      $query = &quot;
      INSERT INTO pdfFiles(Vendor, Category, Notes, Date)  
       VALUES('$vendor', '$category', '$notes', '$date')
      &quot;;
      if(mysqli_query($connect, $query))
      {
       $output .= '&lt;label class=&quot;text-success&quot;&gt;Data Inserted&lt;/label&gt;';
       $select_query = &quot;SELECT * FROM pdfFiles ORDER BY id DESC&quot;;
       $result = mysqli_query($connect, $select_query);
       $output .= '
        &lt;table class=&quot;table table-bordered&quot;&gt;  
                      &lt;tr&gt;  
                           &lt;th width=&quot;25%&quot;&gt;Vendor Name&lt;/th&gt;
                           &lt;th width=&quot;25%&quot;&gt;Category&lt;/th&gt;  
                           &lt;th width=&quot;25%&quot;&gt;Notes&lt;/th&gt;  
                           &lt;th width=&quot;15%&quot;&gt;Date&lt;/th&gt;      
                           &lt;th width=&quot;10%&quot;&gt;View&lt;/th&gt;  
                      &lt;/tr&gt;
  
       ';
       while($row = mysqli_fetch_array($result))
       {
        $output .= '
         &lt;tr&gt;  
                           &lt;td&gt;' . $row[&quot;Vendor&quot;] . '&lt;/td&gt; 
                           &lt;td&gt;' . $row[&quot;Category&quot;] . '&lt;/td&gt; 
                           &lt;td&gt;' . $row[&quot;Notes&quot;] . '&lt;/td&gt;
                           &lt;td&gt;' . $row[&quot;Date&quot;] . '&lt;/td&gt;    
                           &lt;td&gt;&lt;input type=&quot;button&quot; name=&quot;view&quot; value=&quot;view&quot; id=&quot;' . $row[&quot;ID&quot;] . '&quot; class=&quot;btn btn-info btn-xs view_data&quot; /&gt;&lt;/td&gt;  
                      &lt;/tr&gt;
        ';
       }
       $output .= '&lt;/table&gt;';
      }
      echo $output;
  }
  ?&gt;
  </code></pre>
  <p><a href="https://i.stack.imgur.com/OGqoC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OGqoC.png" alt="enter image description here" /></a></p>
  <p><a href="https://i.stack.imgur.com/Fjw3y.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Fjw3y.png" alt="enter image description here" /></a></p>