title: Upload PDF and save to database
body: <p>I am trying to upload a pdf to a database. I keep getting the "error while uploading file" error. Non of the data gets inserted into the database either.</p>
  
  <p>Please can some one who is not so new to PHP and SQL have a look and tell me where I have gone wrong.</p>
  
  <p>At a later stage I will be trying to merge a few pfds together and then save the new combined pdf to the database.</p>
  
  <p>SQL for table</p>
  
  <pre><code>CREATE TABLE `tbl_request` (
    `InvoiceNum` int(11) NOT NULL,
    `userEmail` varchar(25) NOT NULL,
    `Progress` enum('Uploaded','Verified and Awaiting Payment','Submitted to Department','Closed') NOT NULL DEFAULT 'Uploaded',
    `UploadDate` datetime NOT NULL,
    `FileName` varchar(11) NOT NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=COMPACT;
  </code></pre>
  
  <p>PHP code</p>
  
  <pre><code>&lt;?php
  session_start();
  require_once 'class.user.php';
  $user_home = new USER();
  
  if(!$user_home-&gt;is_logged_in())
  {
      $user_home-&gt;redirect('index.php');
  }
  
  if($_SESSION['userRole'] !== ('user'))
  {
      $user_home-&gt;redirect('index.php');
  }
  
  $stmt = $user_home-&gt;runQuery("SELECT * FROM tbl_users WHERE userID=:uid");
  $stmt-&gt;execute(array(":uid"=&gt;$_SESSION['userSession']));
  $row = $stmt-&gt;fetch(PDO::FETCH_ASSOC);
  
  ?&gt;
  
  &lt;!DOCTYPE html&gt;
  &lt;html class="no-js"&gt;
  
      &lt;body&gt;                                  
  &lt;div id="body"&gt;
      &lt;form action="&lt;?php print $_SERVER['PHP_SELF']?&gt;" method="post" enctype="multipart/form-data"&gt;
      &lt;input type="file" name="file" /&gt;
      &lt;button type="submit" name="btn-upload"&gt;upload&lt;/button&gt;
      &lt;/form&gt;
      &lt;br /&gt;&lt;br /&gt;
      &lt;?php
      if(isset($_GET['success']))
      {
          ?&gt;
          &lt;label&gt;File Uploaded Successfully...  &lt;a href="view.php"&gt;click here to view file.&lt;/a&gt;&lt;/label&gt;
          &lt;?php
      }
      else if(isset($_GET['fail']))
      {
          ?&gt;
          &lt;label&gt;Problem While File Uploading !&lt;/label&gt;
          &lt;?php
      }
      else
      {
          ?&gt;
          &lt;label&gt;Please only upload PDF files&lt;/label&gt;
          &lt;?php
      }
      ?&gt;
  &lt;/div&gt;
   &lt;?php
          if(isset($_POST['btn-upload']))
      {  
      if ($_FILES['file']['type'] == "application/pdf") {  
          $update=date("Ymdhis");
          $mail =$row['userEmail'];
          $sql="INSERT INTO tbl_request(userEmail,UploadDate) VALUES('$mail','$update)";
          mysql_query($sql); 
          $file = $update."-".$_FILES['file']['name'];
          $file_loc = $_FILES['file']['tmp_name'];
          $folder="uploads/";
          $new_file_name = strtolower($file);
          $final_file=str_replace(' ','-',$new_file_name);        
          if(move_uploaded_file($file_loc,$folder.$final_file))
          {
              $sql="UPDATE tbl_uploads SET(FileName) VALUES('$final_file') WHERE UploadDate = $update";
              mysql_query($sql);
              ?&gt;
              &lt;script&gt;
              alert('successfully uploaded');
              window.location.href='index.php?success';
              &lt;/script&gt;
              &lt;?php
          }
          else
          {
              ?&gt;
              &lt;script&gt;
              alert('error while uploading file');
              window.location.href='index.php?fail';
              &lt;/script&gt;
              &lt;?php
          }
      }
      else{
      ?&gt;
              &lt;script&gt;
              alert('Only pdf files may be uploaded');
              window.location.href='index.php?fail';
              &lt;/script&gt;
              &lt;?php
      }
  }
  ?&gt;
  
      &lt;/body&gt;    
  &lt;/html&gt;
  </code></pre>