Skip to content

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

   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())";
           echo "CSV File has been successfully Imported";
   echo "Invalid File:Please Upload CSV File";


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
      <title>Import CSV/Excel file</title>
      <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>
               <td align="center">CSV/Excel File:</td>
               <td><input type="file" name="file" id="file"></td>
            <tr >
               <td colspan="2" align="center"><input type="submit" name="Import" value="Import"></td>

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](