duankuixi1930 2014-02-13 14:15
浏览 48
已采纳

使用php将xlsx文件上传到mysql的问题是一个带有所有值的额外字符显示

i am trying to upload an xlsx file. using php and mysql .. but i have got an extra symbol in the database after a value .. like value is ETI5465 after upload it becomes ETI5465Â

if($_POST['action']=="addFile"){
$fname = $_FILES['sel_file']['name'];
        $path_info=pathinfo($fname);
        $ext=$path_info['extension'];




     $chk_ext = explode(".",$fname);

     if(strtolower($chk_ext[1]) == "xlsx")
     {

        require 'simplexlsx.class.php'; 


         $filename = $_FILES['sel_file']['tmp_name'];
       //  $handle = fopen($filename, "r");


            $xlsx = new SimpleXLSX($filename);

            list($cols, $rows) = $xlsx->dimension();


            foreach($xlsx->rows() as $k => $data) { // LOOP THROUGH EXCEL WORKSHEET
                if($k >'7'){

                    if($_POST['upload']=="excel1" &&  $cols<>'10')
                    {


                    $newdate=date('m/d/Y H:i:s A',$xlsx->unixstamp($data[4]));

                    $sql = "INSERT INTO vehicle_fuel_expenses (`company_code_nr`, `cartao`, `placa`, `modelo`, `responsavel`, `data_hora`, `transacao`, `tipo`, `liberacao_Restricao`, `motorista`, `matricula`, `estabelecimento`, `cidade`, `quilometragem`, `horas`, `servico`,`valor`,`km_rodados`,`horas_trabalhadas`,`litros`,`km_litro`,`litros_Hora`,`valor_Litro`,`ia_1`,`ia_2`,`ia_3`,`ia_4`,`ia_5`) VALUES ('".$_SESSION['company']."','$data[0]','$data[1]','$data[2]','$data[3]','$newdate','$data[5]','$data[6]','$data[7]','$data[8]','$data[9]','$data[10]','$data[11]','$data[12]',
                     '$data[13]','$data[14]','$data[15]','$data[16]','$data[17]','$data[18]','$data[19]','$data[20]','$data[21]','$data[22]','$data[23]','$data[24]','$data[25]','$data[26]')";
                    $inser=@mysql_query($sql) or die(mysql_error());
                    }

                        if($_POST['upload']=="excel2" &&  $cols=='3')
                            {

                            //$newdate=date('m/Y',$xlsx->unixstamp($data[4]));
                            $sql = "INSERT INTO vehicle_fuel_expenses_mensalidade1 (`company_code_nr`, `veículo`, `item_faturado`, `valor`) VALUES ('".$_SESSION['company']."','".addslashes($data[0])."','".addslashes($data[1])."','".addslashes($data[2])."')";
                            $inser=@mysql_query($sql) or die(mysql_error());
                            }

                   if($_POST['upload']=="excel3" &&  $cols=='10')
                    {
           $sql = "INSERT INTO vehicle_toll_consumption_cost (`company_code_nr`, `placa`, `tag`, `prefixo`, `marca`, `categ`, `data`, `hora`, `rodovia`, `praca`,   
                    `valor`) VALUES ('".$_SESSION['company']."','$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]','$data[9]')";
                     $inser=@mysql_query($sql) or die(mysql_error());                       

                    }

                        }
                        }         
                    if($inser=='1')
                    {
                        echo '<script>alert("Importado com sucesso");</script>';
                    }
                    else 
                       {
                         echo '<script>alert("Please Select A Valid File");</script>';  
                       }
  • 写回答

1条回答 默认 最新

  • dounie0889 2014-02-25 11:55
    关注

    The problem is due to the encoding of the file and the character set used by the DB tables. For example, if the character set used by the DB tables are utf8_general_ci and the file from which data is read is encoded in UTF-8 (as it probably will be your files) immediately after the connection to the DB you must execute the command SQL to set the correct character set, in this case:

    SET CHARACTER SET utf8
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 高维数据处理方法求指导
  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错
  • ¥15 能把水桶搬到饮水机的机械设计
  • ¥15 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
  • ¥15 使用小程序wx.createWebAudioContext()开发节拍器
  • ¥15 关于#爬虫#的问题:请问HMDB代谢物爬虫的那个工具可以提供一下吗
  • ¥15 vue3+electron打包获取本地视频属性,文件夹里面有ffprobe.exe 文件还会报错这是什么原因呢?
  • ¥20 用51单片机控制急停。