将.xls上传到mysql时处理空值

时间:2011-10-31 20:33:30

标签: php mysql import-from-excel

我有一个网站显示健康测试结果,该结果在Drupal上运行,值存储在mysql数据库中。 oringinal数据来自连接到进行健康测试的人的计算机,然后将其放入excel .xls文件中。

我有一个脚本,允许我将这些.xls文件上传到数据库,这样当我有50-100个测试要添加时,我不必单独完成每个。除了null值之外,它工作得很好。当有空白测试时,它会自动将其设置为0但我希望它设置为空。

对于我而言,null和0之间存在很大差异,因为0表示他们没有得分(例如,无法在卧推时获得所需的重量)与null表示他们没有完成由于诸如伤害或设备不可用等因素而进行的测试。

是否可以区分这两者,以便.xls中的空白单元格在数据库中显示为null而不是0?这是一个代码问题还是数据库问题,我应该尝试将其存储为int或decimal以外的其他东西?

<?php
require_once 'Excel/reader.php';
include('db.php');
global $user;
$useriddd=$user->uid;
$message='';
$userArr=array();
$emailArr=array();
if(isset($_GET['meg']))
  {
     $message=base64_decode($_GET['meg']);
  }
if(isset($_POST['SubmitFile']))
  {
       $temp_path="Excel/";


         if($_FILES['fileexcel']['tmp_name']!="")
          {
            if($_FILES['fileexcel']['name']=='fitness.xls')
              {
               $var1=$_FILES['fileexcel']['name'];
               move_uploaded_file($_FILES['fileexcel']['tmp_name'],$temp_path.$var1);
               $path=$temp_path.$var1;
              }
              else
              {
                $var=base64_encode('You are allowed to upload excel file with name fitness.xls only');
                 ?>
                 <script language="javascript">window.location='<?php echo base_path()?>upload-fitness-results?meg=<?php echo $var;?>'</script>
                 <?php
                 exit;
              }
          }   


  }
if(isset($_GET['p'])=='do')
{

$data = new Spreadsheet_Excel_Reader();

$data->read('Excel/fitness.xls');


error_reporting(E_ALL ^ E_NOTICE);

for ($j =1; $j <= count($data->sheets[0]['cells']); $j++)
     {


       $playeridQry=mysql_query("select uid from users where name='".addslashes($data->sheets[0]['cells'][$j][3])."'")or die(mysql_error());
       $playeridRow=mysql_fetch_array($playeridQry);
       //////////////////////////////////////////////////////////////////////////
       $campidQry=mysql_query("select nid from node where type='training_camp' and title='".addslashes($data->sheets[0]['cells'][$j][2])."'")or die(mysql_error());
       $campidRow=mysql_fetch_array($campidQry);


         /***********************************player content type code***********************************/
$maxqry=mysql_query("select MAX(nid), MAX(vid) from node") or die(mysql_error());
   $maxrow=mysql_fetch_array($maxqry);
   $newnid=$maxrow['MAX(nid)']+1;
   $newvid=$maxrow['MAX(vid)']+1;
 ///////////////////////////for node entry///////////////////////
    $nodeinsertqry="insert into node
                                set
                                vid='".$newvid."',
                                nid='".$newnid."',
                                type='test',
                                title='".addslashes($data->sheets[0]['cells'][$j][1])."',
                                uid='".$useriddd."',
                                created='".time()."',
                                changed='".time()."',
                                status='1'";
    mysql_query($nodeinsertqry) or die(mysql_error());  
    $nodnewid=mysql_insert_id();
    //////////////////////////////for url_alias//////////////////
        $srcc='node/'.$newnid;
        $pathC=str_replace(' ','-',$data->sheets[0]['cells'][$j][1]);
        $dsts='content/'.$pathC;
        $insertAlias=mysql_query("insert into url_alias
                                                    set
                                                    src='".$srcc."',
                                                    dst='".$dsts."'");


              ////////////////revision table//////////////
       $revisioninsertqry="insert into node_revisions
                                set
                                vid='".$newvid."',
                                nid='".$newnid."',

                                title='".addslashes($data->sheets[0]['cells'][$j][1])."',
                                uid='".$useriddd."',

                                format='3',
                                timestamp='".time()."'";

    mysql_query($revisioninsertqry) or die(mysql_error());


         ////////////////////player content type/////////////////

         $playerQry=mysql_query("insert into content_type_test set
                                                                vid='".$newvid."',
                                                                nid='".$newnid."',
                                                                field_player_uid='".$playeridRow['uid']."',
                                                                field_status_value='".addslashes($data->sheets[0]['cells'][$j][4])."',
                                                                field_training_camp_nid='".$campidRow['nid']."',
                                                                field_speed_value='".addslashes($data->sheets[0]['cells'][$j][6])."',
                                                                field_agility_value='".addslashes($data->sheets[0]['cells'][$j][7])."',
                                                                field_trx_value='".addslashes($data->sheets[0]['cells'][$j][5])."',
                                                                field_squats_value='".addslashes($data->sheets[0]['cells'][$j][8])."',
                                                                field_balance_value='".addslashes($data->sheets[0]['cells'][$j][11])."',
                                                                field_bench_value='".addslashes($data->sheets[0]['cells'][$j][10])."',
                                                                field_beep_value='".addslashes($data->sheets[0]['cells'][$j][14])."',
                                                                field_sprint__do_value='".addslashes($data->sheets[0]['cells'][$j][12])."',
                                                                field_hrdropvv_value='".addslashes($data->sheets[0]['cells'][$j][13])."',
                                                               field_chins_value='".addslashes($data->sheets[0]['cells'][$j][9])."',

field_elasticity_value='".addslashes($data->sheets[0]['cells'][$j][15])."'


")or die(mysql_error());    

/********************End of player content type*************************/     








    }

              unlink('Excel/fitness.xls');

                   $message=base64_encode('Data has been populated into data base and file has been deleted from the server');

                   ?>
                 <script language="javascript">window.location='<?php echo base_path()?>upload-fitness-results?meg=<?php echo $message;?>'</script>
                 <?php
                 exit;


}
if(file_exists('Excel/fitness.xls'))
{

  echo "fitness.xls is available....";
  ?>
  <a href="<?php echo base_path()?>upload-fitness-results?p=do">Please Populate the file into database</a>

  <?php

}
if(!file_exists('Excel/fitness.xls'))
{


 ?>
 <form action="" method="post" enctype="multipart/form-data" name="excel" id="excel" >
    <table  style="width:700px; border:#CCCCCC solid 1px;" cellpadding="1" cellspacing="1">
       <tr> 
            <td colspan="2" style="color:#FF0000;">
            <?php
            if(isset($message))
               {
                      echo $message;
               }
            ?>

            </td>
       </tr>
       <tr> 
            <td width="34%" class="subjects">Choose File(fitness.xls) </td>
            <td width="66%"><label>
              <input name="fileexcel" type="file"   id="fileexcel" />
            </label></td>
       </tr>
       <tr>
         <td>&nbsp;</td>
         <td>&nbsp;</td>
      </tr>
       <tr> 
            <td>&nbsp;</td><td><label>
              <input name="SubmitFile" type="submit" class="buttons" id="SubmitFile" value="Upload" />
            </label></td>
       </tr>
    </table>

</form>

 <?php

}   
?>

0 个答案:

没有答案