title: How to upload csv file using php into mysql?
body: <p>How to upload csv file using php into mysql database correctly? </p>
  
  <p>I have a column (uploaded, column name) in phpmyadmin, in this column will store the uploaded csv file.</p>
  
  <p>I have a little script of uploading csv file into mysql database, but I'm getting some error "404: File Not Found" after I clicked the submit button and the csv file is not saving in the database.</p>
  
  <p>Here the php file with html:</p>
  
  <pre><code>&lt;?php
  include "dbase.php"; //Connect to Database
  $deleterecords = "TRUNCATE TABLE contracts"; //empty the table of its current records
   mysql_query($deleterecords);
  //Upload File
  if (isset($_POST['submit'])) {
  if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
      echo "&lt;h1&gt;" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "&lt;/h1&gt;";
      echo "&lt;h2&gt;Displaying contents:&lt;/h2&gt;";
      readfile($_FILES['filename']['tmp_name']);
  }
  //Import uploaded file to Database
  $handle = fopen($_FILES['filename']['tmp_name'], "r");
  while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $import="INSERT into contracts(uploaded) values('$data[0],$data[1]')";
      mysql_query($import) or die(mysql_error());
  }
  fclose($handle);
  echo "Import done";
  //view upload form
  }else {
  echo "Upload new csv by browsing to file and clicking on Upload&lt;br /&gt;\n";
  echo "&lt;form enctype='multipart/form-data' action='upload.php' method='post'&gt;";
  echo "File name to import:&lt;br /&gt;\n";
  echo "&lt;input size='50' type='file' name='filename'&gt;&lt;br /&gt;\n";
  echo "&lt;input type='submit' name='submit' value='Upload'&gt;&lt;/form&gt;";
   }
  ?&gt;
  </code></pre>
  
  <p>Database connection:</p>
  
  <pre><code> &lt;?
   $filename=$_POST['filename'];
   $db=mysql_connect('localhost', 'username', 'password', '3306')
   or die(mysql_error());
   if(!$db) 
  die("no db");
  if(!mysql_select_db("databasename",$db))
  die("No database selected.");
  ?&gt;
  </code></pre>