How to Insert Multiple Record of Excel Sheet using PHP MySQL

  1. <?php  
  2.    define ("DB_HOST""localhost"); // set database host  
  3.    define ("DB_USER""root"); // set database user  
  4.    define ("DB_PASS",""); // set database password  
  5.    define ("DB_NAME","api_db"); // set database name  
  6.    $link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("Couldn't make connection.");  
  7.    $db = mysql_select_db(DB_NAME, $linkor die("Couldn't select database");  
  8.    set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');  
  9.    include 'PHPExcel/IOFactory.php';  
  10.    $uploadedStatus = 0;  
  11.    if ( isset($_POST["submit"]) ) {  
  12.       if ( isset($_FILES["file"])) {  
  13.          //if there was an error uploading the file  
  14.          if ($_FILES["file"]["error"] > 0) {  
  15.             echo "Return Code: " . $_FILES["file"]["error"] . "<br />";  
  16.          }  
  17.          else {  
  18.             $imagename = $_FILES["file"]["name"] ;  
  19.             move_uploaded_file($_FILES["file"]["tmp_name"], $imagename);  
  20.             try {  
  21.                $objPHPExcel = PHPExcel_IOFactory::load($imagename);  
  22.             } catch(Exception $e) {  
  23.             
  24.               die('Error loading file "'.pathinfo($imagename,PATHINFO_BASENAME).'": '.$e->getMessage());  
  25.             }  
  26.             $allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);  
  27.             $arrayCount = count($allDataInSheet); // Here get total count of row in that Excel sheet  
  28.             for($i=2;$i<=$arrayCount;$i++){  
  29.                $userName = trim($allDataInSheet[$i]["A"]);  
  30.                $userMobile = trim($allDataInSheet[$i]["B"]);  
  31.                insertTable= mysql_query("insert into tbl_users (usr_name, usr_email) values('".$userName."', '".$userMobile."');");  
  32.             }  
  33.             echo "Please check your database ";  
  34.          }  
  35.       } else {  
  36.             echo "No file selected <br />";  
  37.          }  
  38.       }  
  39. ?>  
  40. <form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" enctype="multipart/form-data"         <table width="600" style="margin:115px auto; background:#f8f8f8; border:1px solid #eee; padding:10px;">  
  41.       <tr><td colspan="2" style="font:bold 21px arial; text-align:center; border-bottom:1px solid #eee; padding:5px 0 10px 0;">Demo for multiple data store using execel sheet</t  
  42.          <tr><td colspan="2" style="font:bold 15px arial; text-align:center; padding:0 0 5px 0;">Data Uploading System</td></tr>  
  43.             <tr>  
  44.                <td width="50%" style="font:bold 12px tahoma, arial, sans-serif; text-align:right; border-bottom:1px solid #eee; padding:5px 10px 5px 0px; border-right:1px solid #eee;">Select file</td>  
  45.                <td width="50%" style="border-bottom:1px solid #eee; padding:5px;">            
  46.                <input type="file" name="file" id="file" /></td>  
  47.             </tr>  
  48.          </tr>  
  49.          <td style="font:bold 12px tahoma, arial, sans-serif; text-align:right; padding:5px 10px 5px 0px; border-right:1px solid #eee;">Submit</td>  
  50.          <td width="50%" style=" padding:5px;"><input type="submit" name="submit" /></td>  
  51.       </tr>  
  52.    </table>  
  53. </form>