Categories: PHP & Frameworks

Import CSV to Database using PHP

Import csv database using PHP. Follow the following code and you will be done with CSV import script using PHP. If you have any questions you can feel free comment we will reply you ASAP.

PHP Code

  <?php
 
   if(isset($_POST["Import"]))
   {
 
   $filename=$_FILES["file"]["tmp_name"];
 
   if($_FILES["file"]["size"] > 0)
   {
 
   $file = fopen($filename, "r");
           mysql_query('truncate table table');
           while (($data = fgetcsv($file, 10000, ",")) !== FALSE)
           {
              $sql = "INSERT into table(name,email,avg,sr,runs,wickets,hs,sixes,fours,longsixes,created) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]','$data[9]', now())";
              mysql_query($sql);
           }
           fclose($file);
           echo "CSV File has been successfully Imported";
   }
   else
   echo "Invalid File:Please Upload CSV File";
 
   }
   ?>

HTML Code

<!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=iso-8859-1" />
      <title>Import CSV/Excel file</title>
   </head>
   <body>
      <form enctype="multipart/form-data" method="post">
         <table border="1" width="40%" align="center">
            <tr >
               <td colspan="2" align="center"><strong>Import CSV</strong></td>
            </tr>
            <tr>
               <td align="center">CSV/Excel File:</td>
               <td><input type="file" name="file" id="file"></td>
            </tr>
            <tr >
               <td colspan="2" align="center"><input type="submit" name="Import" value="Import"></td>
            </tr>
         </table>
      </form>
   </body>
</html>

You might be wondering why I have given the pre tag brush: js as classes. You need to mention the syntax highlighting that needs to be used to highlight your code. You can find the full list of supported syntax declarations [here](http://alexgorbatchev.com/SyntaxHighlighter/manual/brushes/).

Developer Diary

Share
Published by
Developer Diary

Recent Posts

Laravel vs Node Js: Which One Is Good For What?

Introduction In the world of web development, selecting the correct framework is critical. Laravel and…

3 months ago

Docker Cheatsheet: Essential Commands and Explanations

Introduction By enabling containerization, Docker has transformed the way software is built, deployed, and managed.…

8 months ago

Difference between Memcached and REDIS – Secrets of Caching

Introduction Speed and efficiency are critical in the ever-changing world of web development. Effective caching…

8 months ago

How to Revert a Git Commit: A Simple Example

Introduction Git, a popular version control system in software development, enables developers to track changes,…

8 months ago

What is Git Stash and Why Do You Need It

Introduction Are you tired of grappling with tangled changes and unfinished work while using Git?…

8 months ago

Difference between git stash pop and git stash apply

Introduction Version control is essential in software development because it ensures that changes to a…

8 months ago