无法使用PHP将完整数据从excel上传到数据库

时间:2014-12-09 05:07:20

标签: php

我正在尝试使用php将数据从Excel工作表上传到数据库。我无法将excel表中的完整数据上传到数据库。我只能将10个数据从excel上传到数据库。我的excel表包含20个数据。但只上传10个数字。数组$ allDataInSheet只包含10个值。 我提到了这些http://www.discussdesk.com/import-excel-file-data-in-mysql-database-using-PHP.htm

我的代码如下。

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."');");
    } 
   }
   ?>

0 个答案:

没有答案