Loading A Table From An Uploaded .csv File


<?php

include_once('config.php');

//----- CONNECT TO DATABASE -----
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") )
{
  //----------------------------------------
  //----- UPLOADING NEW 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
                  (
                      '".addslashes($track_name_search_by)."',
                      '".addslashes($track_name)."',
                      '".addslashes($artist)."'
                  )
              ");
          }
      } 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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Upload Page</title>
</head>

<body>
  <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" />
  </form> 


  <div style="padding-top: 20px;"></div>
  <h2>All Songs</h2>
  <?php
    $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>";
    }
    else
    {
      while ($result1 = @mysql_fetch_array($query1))
      {
        ?>
        
        <div>
          <?php echo $result1['track_name'] . ' by ';?>
          <?php echo $result1['artist'];?><br />
        </div>

        <?php
      }
    }
  ?>
  
</body>
</html>

 

USEFUL?
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.

Comments

Your email address will not be published.