title: Uploading data and files to MySQL database through a single PHP form
body: <p>I am having some input types in a HTML form and a file uploader. But I am unable to upload both data and file at the same time in the MySQL database. Every time blank entries are filled up into the database...</p>
  
  <p><strong>HTML Form</strong></p>
  
  <pre><code>&lt;form method="post" action="upload.php" enctype="multipart/form-data"&gt;
      &lt;p&gt;
          &lt;label&gt;Name:&lt;/label&gt;
          &lt;input type="text" name="name" size="40"&gt;
      &lt;/p&gt;
      &lt;p&gt;
          &lt;label&gt;Email:&lt;/label&gt;
          &lt;input type="email" name="email"&gt;
      &lt;/p&gt;
      &lt;p&gt;
          &lt;input type="radio" name="gen" value="Male"&gt;Male
          &lt;input type="radio" name="gen" value="Female"&gt;Female&lt;/p&gt;
      &lt;input type="hidden" name="MAX_FILE_SIZE" value="1000000"&gt;
      &lt;br&gt;File to upload:
      &lt;br&gt;
      &lt;input type="file" name="File" size="40"&gt;
      &lt;p&gt;
          &lt;input type="submit" name="submit" value="submit"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p><strong>PHP Code</strong></p>
  
  <pre><code>if(isset($_POST['name']) &amp;&amp; isset($_POST['email']) &amp;&amp;
          isset($_POST['gen']) &amp;&amp; $_FILES['File']['size'] &gt; 0) {
      $name=get_POST('name');
      $email=get_POST('email');
      $gender=get_POST('gen');
      $filename = $_FILES['File']['name'];
      $tmpname  = $_FILES['File']['tmp_name'];
      $filesize = $_FILES['File']['size'];
      $filetype = $_FILES['File']['type'];
      $fp = fopen($tmpname, 'r');
      $file = fread($fp, filesize($tmpname));
      $file = addslashes($file);
      fclose($fp);
  
      if(!get_magic_quotes_gpc()) {
          $filename = addslashes($filename);
      }
      $query = "
          INSERT INTO uploadform
          (Name, Email, Gender, Filename, Filetype, Filesize, File) VALUES
          ('$name', '$email', '$gender', '$filename', '$filetype', '$filesize', '$file');";
  
      mysql_query($query) or die('Error, query failed');
  }
  </code></pre>
  
  <p>In the database the datatype of the file is <code>MEDIUMBLOB</code></p>