Loading A Table From An Uploaded .csv File



mysql_connect("localhost", $songs_db_username, $songs_db_password, $songs_db_name) or die("cannot connect");
mysql_select_db($songs_db_name) or die(mysql_error());		//Select the database so it doesn't need to be specified in mysql functions

if ( (isset($_POST['action'])) && ($_POST['action'] == "uploading_all_songs_file") )
  if ($_FILES[csv][size] > 0)
      //get the csv file
      $file = $_FILES[csv][tmp_name];
      $handle = fopen($file,"r");
      //Delete the existing records in table
      $result = @mysql_query("TRUNCATE TABLE all_songs");
      //Loop through the csv file and insert into database
      do {
          if ($data[0])
            $track_name_search_by = trim(@mysql_real_escape_string($data[0]));
            if (strlen($track_name_search_by) > 254)
              $track_name_search_by = substr($track_name_search_by, 0, 254); 
            $track_name = trim(@mysql_real_escape_string($data[1]));
            if (strlen($track_name) > 254)
              $track_name = substr($track_name, 0, 254); 
            $artist = trim(@mysql_real_escape_string($data[2]));
            if (strlen($artist) > 254)
              $artist = substr($artist, 0, 254); 
            mysql_query("INSERT INTO all_songs (track_name_search_by, track_name, artist) VALUES
      } while ($data = fgetcsv($handle,1000,",","'"));

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Upload Page</title>

  <div style="padding-top: 20px;"></div>
  <h2>Upload New all_songs.csv File</h2>
  <form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
    Choose your file: <br />
    <input name="csv" type="file" id="csv" />
    <input name="action" type="hidden" value="uploading_all_songs_file" />
    <input type="submit" name="Submit" value="Submit" />

  <div style="padding-top: 20px;"></div>
  <h2>All Songs</h2>
    $randomiser_counter = 0;
    $query1 = @mysql_query("SELECT * FROM all_songs ORDER BY track_name_search_by ASC");
    echo 'Total Number Of All Song Results: ' . mysql_num_rows($query1);
    if (@mysql_num_rows($query1) == 0)
      echo "<div style=\"text-align: center;margin-top: 30px;margin-bottom: 30px;\">";
      echo "<p>None found</p>";
      echo "</div>";
      while ($result1 = @mysql_fetch_array($query1))
          <?php echo $result1['track_name'] . ' by ';?>
          <?php echo $result1['artist'];?><br />



We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.


Your email address will not be published.