Home:ALL Converter>Cant upload full data from excel to database using PHP

Cant upload full data from excel to database using PHP

Ask Time:2014-12-09T13:07:20         Author:sh kk

Json Formatter

I am trying for upload data from excel sheet to database using php. I cant upload full data from excel sheet to database.I can upload only 10 data from excel to database.My excel sheet contain 20 data.But only upload 10 numbers.The array $allDataInSheet contain only 10 values. I refered these http://www.discussdesk.com/import-excel-file-data-in-mysql-database-using-PHP.htm.

My code is given below.

main.php

    <form action="<?php echo $_SERVER["PHP_SELF"]; ?>" 
    method="post"   enctype="multipart/form-data">
   <input type="file" name="file" id="file" />
   <input type="button" name="submit"  onClick="file_up()"/>
   </form> 
   <srcipt>

   function file_up()
    {
    var file=$('#file').val();
    var cell=$('#cell').val();
    $.ajax({
    type: "POST",
    url: 'ajx_upload.php',
    data:'file='+file + '&cell=' + cell,
    success: function(msg)
     { 
       $("#upload_show").html(msg);
     }
   });
  }
 </script>

ajx_upload.php

   <?php
   error_reporting(0);
   $uploadedStatus = 0;
   $cell=$_POST['cell'];
   $file1=$_POST['file'];
   $file1 = str_replace("\\", "\\", $file1);
   $file1 = explode("\\",$file1);
   $file=$file1[2];

   if (file_exists($file)) {
    unlink($file);
     }
   $storagename = "discussdesk.xlsx";
   move_uploaded_file($file,  $storagename);
   $uploadedStatus = 1;


   if($uploadedStatus==1)
   {
     define ("DB_HOST", "localhost"); // set database host
     define ("DB_USER", "root"); // set database user
     define ("DB_PASS",""); // set database password
     define ("DB_NAME","excel"); // set database name

     $link = @mysql_connect(DB_HOST, DB_USER, DB_PASS) or 
    die("Couldn't make       connection.");
    $db = mysql_select_db(DB_NAME, $link) or die("Couldn't select database");

    $databasetable = "first";

     set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');
    include 'PHPExcel/IOFactory.php';

    // This is the file path to be uploaded.
   $inputFileName = 'discussdesk.xlsx'; 

    try {
   $objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
   } catch(Exception $e) {
   die('Error loading file "'.
    pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
    }

   $allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
   $arrayCount = count($allDataInSheet);  
   // Here get total count of row in that        Excel sheet
  //print_r($allDataInSheet);
  for($i=2;$i<=$arrayCount;$i++)
  {
   $userName = trim($allDataInSheet[$i]["A"]);
   $userMobile = trim($allDataInSheet[$i]["B"]);
   $query = "SELECT name FROM first WHERE name = '".$userName."'
    and email =         '".$userMobile."'";
   $sql = mysql_query($query);
   $recResult = mysql_fetch_array($sql);
   $existName = $recResult["name"];
   if($existName=="") 
    {
          $insertTable= mysql_query("insert into 
          first (name, email)  values('".$userName."', '".$userMobile."');");
    } 
   }
   ?>

Author:sh kk,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/27371814/cant-upload-full-data-from-excel-to-database-using-php
yy