{"id":1056,"date":"2013-09-10T21:54:13","date_gmt":"2013-09-10T21:54:13","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=1056"},"modified":"2022-02-17T07:14:03","modified_gmt":"2022-02-17T07:14:03","slug":"importing-mysql-table-from-an-uploaded-csv-file","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/php\/files\/importing-mysql-table-from-an-uploaded-csv-file","title":{"rendered":"Importing MySQL Table From An Uploaded CSV File"},"content":{"rendered":"<h4>\nLoading A Table From An Uploaded .csv File<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n&lt;?php\r\n\r\ninclude_once(&#39;config.php&#39;);\r\n\r\n\/\/----- CONNECT TO DATABASE -----\r\nmysql_connect(&quot;localhost&quot;, $songs_db_username, $songs_db_password, $songs_db_name) or die(&quot;cannot connect&quot;);\r\nmysql_select_db($songs_db_name) or die(mysql_error());\t\t\/\/Select the database so it doesn&#39;t need to be specified in mysql functions\r\n\r\n\r\nif ( (isset($_POST['action'])) &amp;&amp; ($_POST['action'] == &quot;uploading_all_songs_file&quot;) )\r\n{\r\n  \/\/----------------------------------------\r\n  \/\/----- UPLOADING NEW ALL SONGS FILE -----\r\n  \/\/----------------------------------------\r\n  if ($_FILES[csv][size] &gt; 0)\r\n  {\r\n      \/\/get the csv file\r\n      $file = $_FILES[csv][tmp_name];\r\n      $handle = fopen($file,&quot;r&quot;);\r\n      \r\n      \/\/Delete the existing records in table\r\n      $result = @mysql_query(&quot;TRUNCATE TABLE all_songs&quot;);\r\n      \r\n      \/\/Loop through the csv file and insert into database\r\n      do {\r\n          if ($data[0])\r\n          {\r\n            $track_name_search_by = trim(@mysql_real_escape_string($data[0]));\r\n            if (strlen($track_name_search_by) &gt; 254)\r\n              $track_name_search_by = substr($track_name_search_by, 0, 254); \r\n              \r\n            $track_name = trim(@mysql_real_escape_string($data[1]));\r\n            if (strlen($track_name) &gt; 254)\r\n              $track_name = substr($track_name, 0, 254); \r\n              \r\n            $artist = trim(@mysql_real_escape_string($data[2]));\r\n            if (strlen($artist) &gt; 254)\r\n              $artist = substr($artist, 0, 254); \r\n              \r\n            mysql_query(&quot;INSERT INTO all_songs (track_name_search_by, track_name, artist) VALUES\r\n                  (\r\n                      &#39;&quot;.addslashes($track_name_search_by).&quot;&#39;,\r\n                      &#39;&quot;.addslashes($track_name).&quot;&#39;,\r\n                      &#39;&quot;.addslashes($artist).&quot;&#39;\r\n                  )\r\n              &quot;);\r\n          }\r\n      } while ($data = fgetcsv($handle,1000,&quot;,&quot;,&quot;&#39;&quot;));\r\n  }\r\n}\r\n\r\n?&gt;\r\n&lt;!DOCTYPE html PUBLIC &quot;-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN&quot; &quot;http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd&quot;&gt;\r\n&lt;html xmlns=&quot;http:\/\/www.w3.org\/1999\/xhtml&quot;&gt;\r\n&lt;head&gt;\r\n&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text\/html; charset=utf-8&quot; \/&gt;\r\n\r\n&lt;title&gt;Upload Page&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n\r\n&lt;body&gt;\r\n  &lt;div style=&quot;padding-top: 20px;&quot;&gt;&lt;\/div&gt;\r\n  &lt;h2&gt;Upload New all_songs.csv File&lt;\/h2&gt;\r\n  &lt;form action=&quot;&quot; method=&quot;post&quot; enctype=&quot;multipart\/form-data&quot; name=&quot;form1&quot; id=&quot;form1&quot;&gt;\r\n    Choose your file: &lt;br \/&gt;\r\n    &lt;input name=&quot;csv&quot; type=&quot;file&quot; id=&quot;csv&quot; \/&gt;\r\n    &lt;input name=&quot;action&quot; type=&quot;hidden&quot; value=&quot;uploading_all_songs_file&quot; \/&gt;\r\n    &lt;input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;Submit&quot; \/&gt;\r\n  &lt;\/form&gt; \r\n\r\n\r\n  &lt;div style=&quot;padding-top: 20px;&quot;&gt;&lt;\/div&gt;\r\n  &lt;h2&gt;All Songs&lt;\/h2&gt;\r\n  &lt;?php\r\n    $randomiser_counter = 0;\r\n    $query1 = @mysql_query(&quot;SELECT * FROM all_songs ORDER BY track_name_search_by ASC&quot;);\r\n    echo &#39;Total Number Of All Song Results: &#39; . mysql_num_rows($query1);\r\n    if (@mysql_num_rows($query1) == 0)\r\n    {\r\n      echo &quot;&lt;div style=\\&quot;text-align: center;margin-top: 30px;margin-bottom: 30px;\\&quot;&gt;&quot;;\r\n      echo &quot;&lt;p&gt;None found&lt;\/p&gt;&quot;;\r\n      echo &quot;&lt;\/div&gt;&quot;;\r\n    }\r\n    else\r\n    {\r\n      while ($result1 = @mysql_fetch_array($query1))\r\n      {\r\n        ?&gt;\r\n        \r\n        &lt;div&gt;\r\n          &lt;?php echo $result1['track_name'] . &#39; by &#39;;?&gt;\r\n          &lt;?php echo $result1['artist'];?&gt;&lt;br \/&gt;\r\n        &lt;\/div&gt;\r\n\r\n        &lt;?php\r\n      }\r\n    }\r\n  ?&gt;\r\n  \r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/code><\/pre>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Loading A Table From An Uploaded .csv File &lt;?php include_once(&#39;config.php&#39;); \/\/&#8212;&#8211; CONNECT TO DATABASE &#8212;&#8211; mysql_connect(&quot;localhost&quot;, $songs_db_username, $songs_db_password, $songs_db_name) or die(&quot;cannot connect&quot;); mysql_select_db($songs_db_name) or die(mysql_error()); \/\/Select the database so it doesn&#39;t need to be specified in mysql functions if ( (isset($_POST[&#8216;action&#8217;])) &amp;&amp; ($_POST[&#8216;action&#8217;] == &quot;uploading_all_songs_file&quot;) ) { \/\/&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- \/\/&#8212;&#8211; UPLOADING NEW ALL SONGS FILE &#8212;&#8211; [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[58,110],"tags":[],"class_list":["post-1056","post","type-post","status-publish","format-standard","hentry","category-files","category-import"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/1056","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=1056"}],"version-history":[{"count":5,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/1056\/revisions"}],"predecessor-version":[{"id":1336,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/1056\/revisions\/1336"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=1056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=1056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=1056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}