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

Git Tag Cheat Sheet

Introduction Git tags are an essential feature of version control systems, offering a simple way…

4 months ago

Understanding Web Storage: Cookies, Local Storage

Introduction The methods that browsers employ to store data on a user's device are referred…

4 months ago

Setting up OpenVPN Access Server in Amazon VPC – AWS

Introduction A well-known open-source VPN technology, OpenVPN provides strong protection for both people and businesses.…

4 months ago

Enhance Error Tracking & Monitoring: Integrate Sentry with Node.js & Express.js

Introduction Integrating Sentry into a Node.js, Express.js, and MongoDB backend project significantly enhances error tracking…

4 months ago

Comparing Callbacks, Promises, and Async/Await in JavaScript

Introduction In the world of JavaScript development, efficiently managing asynchronous operations is essential. Asynchronous programming…

6 months ago

How To Secure Nginx with Let’s Encrypt on Ubuntu EC2 Instance

Introduction Let's Encrypt is a Certificate Authority (CA) that makes it simple to obtain and…

8 months ago